/** * Use the provided byte[] to buffer only the bytes used to write out the integer i to the * DataOutput out. This will only ever make one write call to the DataOutput. Use this instead of * {@link WritableUtils#writeVInt(DataOutput, int)} which could make up to 4 separate writes to * the underlying OutputStream. Is compatible with WritableUtils as it will write the same data. */ public static void writeVInt(DataOutput out, byte[] workBuffer, int i) throws IOException { int size = UnsynchronizedBuffer.writeVInt(workBuffer, 0, i); out.write(workBuffer, 0, size); }
@Override public void write(DataOutput out) throws IOException { final byte[] integerBuffer = new byte[5]; serialize(); byte hasValues = (values == null) ? 0 : (byte) 1; if (!replicationSources.isEmpty()) { // Use 2nd least-significant bit for whether or not we have replication sources hasValues = (byte) (0x02 | hasValues); } out.write((byte) (0x80 | hasValues)); UnsynchronizedBuffer.writeVInt(out, integerBuffer, row.length); out.write(row); UnsynchronizedBuffer.writeVInt(out, integerBuffer, data.length); out.write(data); UnsynchronizedBuffer.writeVInt(out, integerBuffer, entries); if ((0x01 & hasValues) == 0x01) { UnsynchronizedBuffer.writeVInt(out, integerBuffer, values.size()); for (byte[] val : values) { UnsynchronizedBuffer.writeVInt(out, integerBuffer, val.length); out.write(val); } } if ((0x02 & hasValues) == 0x02) { UnsynchronizedBuffer.writeVInt(out, integerBuffer, replicationSources.size()); for (String source : replicationSources) { WritableUtils.writeString(out, source); } } }