From 9b0a3cf498921fbfe585b110d8ffb387881cd27d Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Mon, 10 Aug 2020 18:35:45 -0700 Subject: [PATCH] Filled remaining methods and changed write methods to support chaining --- core/src/com/riiablo/io/ByteOutput.java | 43 ++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/core/src/com/riiablo/io/ByteOutput.java b/core/src/com/riiablo/io/ByteOutput.java index 87b13a5b..ecdd69dd 100644 --- a/core/src/com/riiablo/io/ByteOutput.java +++ b/core/src/com/riiablo/io/ByteOutput.java @@ -1,12 +1,23 @@ package com.riiablo.io; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.util.CharsetUtil; public class ByteOutput { + public static ByteOutput wrap(byte[] bytes) { + return wrap(Unpooled.wrappedBuffer(bytes)); + } + + public static ByteOutput wrap(ByteBuf buffer) { + return new ByteOutput(buffer); + } + private final ByteBuf buffer; private BitOutput bitOutput; ByteOutput(ByteBuf buffer) { + assert !buffer.isReadOnly(); this.buffer = buffer; } @@ -23,7 +34,7 @@ public class ByteOutput { } public boolean aligned() { - throw null; + return bitOutput == null || bitOutput.aligned(); } public BitOutput unalign() { @@ -47,27 +58,49 @@ public class ByteOutput { buffer.writeByte((int) octet); } - public void write8(int value) { + public ByteOutput write8(int value) { assert aligned() : "not aligned"; incrementBitsWritten(Byte.SIZE); buffer.writeByte(value); + return this; } - public void write16(int value) { + public ByteOutput write16(int value) { assert aligned() : "not aligned"; incrementBitsWritten(Short.SIZE); buffer.writeShortLE(value); + return this; } - public void write32(int value) { + public ByteOutput write32(int value) { assert aligned() : "not aligned"; incrementBitsWritten(Short.SIZE); buffer.writeIntLE(value); + return this; } - public void write64(long value) { + public ByteOutput write64(long value) { assert aligned() : "not aligned"; incrementBitsWritten(Short.SIZE); buffer.writeLongLE(value); + return this; + } + + public ByteOutput writeBytes(byte[] src) { + assert aligned() : "not aligned"; + return writeBytes(src, 0, src.length); + } + + public ByteOutput writeBytes(byte[] src, int srcOffset, int len) { + assert aligned() : "not aligned"; + incrementBitsWritten((long) len * Byte.SIZE); + buffer.writeBytes(src, srcOffset, len); + return this; + } + + public ByteOutput writeString(CharSequence chars) { + incrementBitsWritten((long) chars.length() * Byte.SIZE); + buffer.writeCharSequence(chars, CharsetUtil.US_ASCII); + return this; } }