@Override public void writeInt(int v) throws IOException { getDataOutputToWriteTo().writeInt(v); }
/** * Reset the stream */ private void reset() { dataOutput.reset(); // reserve the place for count to be written in the end try { dataOutput.writeInt(0); } catch (IOException e) { throw new IllegalStateException("reset: Got IOException", e); } counter = 0; } }
/** * Returns all the data from the stream and clears it. * * @return Number of objects followed by the data written to the stream */ public byte[] flush() { dataOutput.writeInt(0, counter); // Actual flush not required, this is a byte array byte[] ret = dataOutput.toByteArray(); reset(); return ret; }
/** * Add a message. * The order is: the message>id count>ids . * * @param ids The byte array which holds target ids * of this message on the worker * @param idPos The end position of the ids * information in the byte array above. * @param count The number of ids * @param msg The message sent */ public void add(byte[] ids, int idPos, int count, M msg) { try { msg.write(extendedDataOutput); extendedDataOutput.writeInt(count); extendedDataOutput.write(ids, 0, idPos); } catch (IOException e) { throw new IllegalStateException("add: IOException", e); } }
/** * Write object to a byte array with the first 4 bytes as the size of the * entire buffer (including the size). * * @param writableObject Object to write from. * @param buffer Use this buffer instead * @param unsafe Use unsafe serialization? * @return Byte array with serialized object. */ public static byte[] writeToByteArrayWithSize(Writable writableObject, byte[] buffer, boolean unsafe) { ExtendedDataOutput extendedDataOutput; if (unsafe) { extendedDataOutput = new UnsafeByteArrayOutputStream(buffer); } else { extendedDataOutput = new ExtendedByteArrayDataOutput(buffer); } try { extendedDataOutput.writeInt(-1); writableObject.write(extendedDataOutput); extendedDataOutput.writeInt(0, extendedDataOutput.getPos()); } catch (IOException e) { throw new IllegalStateException("writeToByteArrayWithSize: " + "IOException", e); } return extendedDataOutput.getByteArray(); }
/** * Write a size of the message and message * * @param message Message to write */ private void writeMessageWithSize(M message) throws IOException { int pos = extendedDataOutput.getPos(); extendedDataOutput.skipBytes(4); writeData(extendedDataOutput, message); extendedDataOutput.writeInt( pos, extendedDataOutput.getPos() - pos - 4); }