/** * Write one of data outputs to another data output * * @param dataOutput Data output to write * @param out Data output to write to */ private void writeExtendedDataOutput(ExtendedDataOutput dataOutput, DataOutput out) throws IOException { out.writeInt(dataOutput.getPos()); out.write(dataOutput.getByteArray(), 0, dataOutput.getPos()); }
/** * 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); }
/** * 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(); }
@Override public boolean isEmpty() { return extendedDataOutput.getPos() == 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); } }
@Override public void add(byte[] serializedId, int idPos, T data) { try { extendedDataOutput.write(serializedId, 0, idPos); writeData(extendedDataOutput, data); } catch (IOException e) { throw new IllegalStateException("add: IOException", e); } }
/** * Write global communication object to the stream * and increment internal counter * * @param name Name * @param type Global communication type * @param value Object value * @return Number of bytes occupied by the stream * @throws IOException */ public int addValue(String name, GlobalCommType type, Writable value) throws IOException { incrementCounter(); dataOutput.writeUTF(name); dataOutput.writeByte(type.ordinal()); if (writeClass) { WritableUtils.writeWritableObject(value, dataOutput); } else { value.write(dataOutput); } return getSize(); } }
/** * Get the underlying byte-array. * * @return The underlying byte-array */ public byte[] getByteArray() { return extendedDataOutput.getByteArray(); }
/** * Reset ExtendedDataOutput array for id serialization * in next message-Vids encoding */ private void resetIdSerializers() { for (int i = 0; i < this.idSerializer.length; i++) { if (idSerializer[i] != null) { idSerializer[i].reset(); } } }
/** * Get resulting byte array * @return resulting byte array */ byte[] toByteArray() { return out.toByteArray(); } }
@Override public void writeByte(int v) throws IOException { getDataOutputToWriteTo().writeByte(v); }
@Override public void writeBoolean(boolean v) throws IOException { getDataOutputToWriteTo().writeBoolean(v); }
@Override public void writeChar(int v) throws IOException { getDataOutputToWriteTo().writeChar(v); }
@Override public void writeBytes(String s) throws IOException { getDataOutputToWriteTo().writeBytes(s); }
extendedDataOutput.writeInt(-1); writeVertexToDataOutput(extendedDataOutput, vertex, conf); extendedDataOutput.writeInt(0, extendedDataOutput.getPos()); } catch (IOException e) { throw new IllegalStateException("writeVertexToByteArray: " + return extendedDataOutput.getByteArray();
@Override protected int entrySerializedSize(ExtendedDataOutput vertices) { return vertices.getPos(); } }
@Override public void write(byte[] b) throws IOException { getDataOutputToWriteTo().write(b); }
@Override public void writeCurrentMessageBytes(DataOutput dataOutput) { try { dataOutput.write(extendedDataOutput.getByteArray(), messageOffset, messageBytes); } catch (NegativeArraySizeException e) { VerboseByteStructMessageWrite.handleNegativeArraySize(vertexId); } catch (IOException e) { throw new IllegalStateException("writeCurrentMessageBytes: Got " + "IOException", e); } } };