/** * Writes a variable int directly to a byte array. Is compatible with {@link WritableUtils} as it * will write the same data. */ public static int writeVInt(byte[] dest, int offset, int i) { return writeVLong(dest, offset, i); }
private void reserve(int l) { if (offset + l > data.length) { int newSize = UnsynchronizedBuffer.nextArraySize(offset + l); byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, offset); data = newData; } }
/** * 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); }
/** * Use the provided byte[] to buffer only the bytes used to write out the long i to the DataOutput * out. This will only ever make one write call to the DataOutput. Use this instead of * {@link WritableUtils#writeVLong(DataOutput, long)} which could make up to 8 separate writes to * the underlying OutputStream. Is compatible with WritableUtils as it will write the same data. */ public static void writeVLong(DataOutput out, byte[] workBuffer, long i) throws IOException { int size = UnsynchronizedBuffer.writeVLong(workBuffer, 0, i); out.write(workBuffer, 0, size); }
private static void read(DataInput in, MutableByteSequence mbseqDestination, int len) throws IOException { if (mbseqDestination.getBackingArray().length < len) { mbseqDestination.setArray(new byte[UnsynchronizedBuffer.nextArraySize(len)], 0, 0); } in.readFully(mbseqDestination.getBackingArray(), 0, len); mbseqDestination.setLength(len); }
@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); } } }
@Override public void write(DataOutput out) throws IOException { final byte[] timeBuffer = new byte[9]; super.write(out); UnsynchronizedBuffer.writeVLong(out, timeBuffer, systemTime); }
private static void readPrefix(DataInput in, MutableByteSequence dest, ByteSequence prefixSource) throws IOException { int prefixLen = WritableUtils.readVInt(in); int remainingLen = WritableUtils.readVInt(in); int len = prefixLen + remainingLen; if (dest.getBackingArray().length < len) { dest.setArray(new byte[UnsynchronizedBuffer.nextArraySize(len)], 0, 0); } if (prefixSource.isBackedByArray()) { System.arraycopy(prefixSource.getBackingArray(), prefixSource.offset(), dest.getBackingArray(), 0, prefixLen); } else { byte[] prefixArray = prefixSource.toArray(); System.arraycopy(prefixArray, 0, dest.getBackingArray(), 0, prefixLen); } // read remaining in.readFully(dest.getBackingArray(), prefixLen, remainingLen); dest.setLength(len); }
/** * Adds a long value to this writer's buffer. The long is encoded as a variable-length list of * bytes. For a description of the encoding scheme, see <code>WritableUtils.writeVLong()</code> * in the Hadoop API. [<a href= * "http://hadoop.apache.org/docs/stable/api/org/apache/hadoop/io/WritableUtils.html#writeVLong%28java.io.DataOutput,%20long%29">link</a>] * * @param i * long value */ public void writeVLong(long i) { reserve(9); offset = UnsynchronizedBuffer.writeVLong(data, offset, i); }
private void reserve(int l) { if (offset + l > data.length) { int newSize = UnsynchronizedBuffer.nextArraySize(offset + l); byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, offset); data = newData; } }
private static void read(DataInput in, MutableByteSequence mbseqDestination, int len) throws IOException { if (mbseqDestination.getBackingArray().length < len) { mbseqDestination.setArray(new byte[UnsynchronizedBuffer.nextArraySize(len)], 0, 0); } in.readFully(mbseqDestination.getBackingArray(), 0, len); mbseqDestination.setLength(len); }
private static void readPrefix(DataInput in, MutableByteSequence dest, ByteSequence prefixSource) throws IOException { int prefixLen = WritableUtils.readVInt(in); int remainingLen = WritableUtils.readVInt(in); int len = prefixLen + remainingLen; if (dest.getBackingArray().length < len) { dest.setArray(new byte[UnsynchronizedBuffer.nextArraySize(len)], 0, 0); } if (prefixSource.isBackedByArray()) { System.arraycopy(prefixSource.getBackingArray(), prefixSource.offset(), dest.getBackingArray(), 0, prefixLen); } else { byte[] prefixArray = prefixSource.toArray(); System.arraycopy(prefixArray, 0, dest.getBackingArray(), 0, prefixLen); } // read remaining in.readFully(dest.getBackingArray(), prefixLen, remainingLen); dest.setLength(len); }