/** * Write a 32 bit integer to the byte array without allocating memory. * @param value a 32 bit integer. */ private void writeInteger32ToByteArray(int value) { //byte[] intBytes = new byte[4]; //I allocated the this buffer globally so the GC has less work intBytes[3] = (byte)value; value >>>= 8; intBytes[2] = (byte)value; value >>>= 8; intBytes[1] = (byte)value; value >>>= 8; intBytes[0] = (byte)value; writeUnderHandler(intBytes); }
/** * Write a 64 bit integer to the byte array without allocating memory. * @param value a 64 bit integer. */ private void writeInteger64ToByteArray(long value) { longintBytes[7] = (byte)value; value >>>= 8; longintBytes[6] = (byte)value; value >>>= 8; longintBytes[5] = (byte)value; value >>>= 8; longintBytes[4] = (byte)value; value >>>= 8; longintBytes[3] = (byte)value; value >>>= 8; longintBytes[2] = (byte)value; value >>>= 8; longintBytes[1] = (byte)value; value >>>= 8; longintBytes[0] = (byte)value; writeUnderHandler(longintBytes); } }
/** * Write a string into the byte stream. * @param aString the string to be written */ public void write(String aString) { final byte[] stringBytes = aString.getBytes(charset); writeUnderHandler(stringBytes); stream.write(0); alignStream(); }
/** * Write bytes into the byte stream. * @param bytes bytes to be written */ public void write(byte[] bytes) { writeInteger32ToByteArray(bytes.length); writeUnderHandler(bytes); alignStream(); }