@Override public void write(ByteBuffer b, int off, int len) throws IOException { ByteBufferUtils.copyBufferToStream(out, b, off, len); written += len; } }
@Override public int write(OutputStream out, boolean withTags) throws IOException { int length = getSerializedSize(withTags); ByteBufferUtils.copyBufferToStream(out, this.buf, this.offset, length); return length; }
/** * Copy the data to the output stream and update position in buffer. * @param out the stream to write bytes to * @param in the buffer to read bytes from * @param length the number of bytes to copy */ public static void moveBufferToStream(OutputStream out, ByteBuffer in, int length) throws IOException { copyBufferToStream(out, in, in.position(), length); skip(in, length); }
@Override public int write(OutputStream out, boolean withTags) throws IOException { int lenToWrite = getSerializedSize(withTags); ByteBufferUtils.putInt(out, keyBuffer.capacity()); ByteBufferUtils.putInt(out, valueLength); // Write key out.write(keyBuffer.array()); // Write value ByteBufferUtils.copyBufferToStream(out, this.valueBuffer, this.valueOffset, this.valueLength); if (withTags && this.tagsLength > 0) { // 2 bytes tags length followed by tags bytes // tags length is serialized with 2 bytes only(short way) even if the type is int. // As this is non -ve numbers, we save the sign bit. See HBASE-11437 out.write((byte) (0xff & (this.tagsLength >> 8))); out.write((byte) (0xff & this.tagsLength)); ByteBufferUtils.copyBufferToStream(out, this.tagsBuffer, this.tagsOffset, this.tagsLength); } return lenToWrite; }
/** * Writes the family from the given cell to the output stream * @param out The outputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param flength the family length * @throws IOException */ public static void writeFamily(OutputStream out, Cell cell, byte flength) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getFamilyByteBuffer(), ((ByteBufferExtendedCell) cell).getFamilyPosition(), flength); } else { out.write(cell.getFamilyArray(), cell.getFamilyOffset(), flength); } }
/** * Helper methods to write the dictionary data to the OutputStream * @param out the outputstream to which data needs to be written * @param data the data to be written in ByteBuffer * @param offset the offset * @param length length to be written * @param dict the dictionary whose contents are to written * @throws IOException */ public static void write(OutputStream out, ByteBuffer data, int offset, int length, Dictionary dict) throws IOException { short dictIdx = Dictionary.NOT_IN_DICTIONARY; if (dict != null) { dictIdx = dict.findEntry(data, offset, length); } if (dictIdx == Dictionary.NOT_IN_DICTIONARY) { out.write(Dictionary.NOT_IN_DICTIONARY); StreamUtils.writeRawVInt32(out, length); ByteBufferUtils.copyBufferToStream(out, data, offset, length); } else { StreamUtils.writeShort(out, dictIdx); } } }
/** * Writes the row from the given cell to the output stream * @param out The outputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param rlength the row length * @throws IOException */ public static void writeRow(OutputStream out, Cell cell, short rlength) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getRowByteBuffer(), ((ByteBufferExtendedCell) cell).getRowPosition(), rlength); } else { out.write(cell.getRowArray(), cell.getRowOffset(), rlength); } }
/** * Writes the value from the given cell to the output stream * @param out The outputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param vlength the value length * @throws IOException */ public static void writeValue(OutputStream out, Cell cell, int vlength) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getValueByteBuffer(), ((ByteBufferExtendedCell) cell).getValuePosition(), vlength); } else { out.write(cell.getValueArray(), cell.getValueOffset(), vlength); } }
/** * Writes the qualifier from the given cell to the output stream excluding the common prefix * @param out The dataoutputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param qlength the qualifier length * @throws IOException * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. */ @Deprecated public static void writeQualifierSkippingBytes(DataOutputStream out, Cell cell, int qlength, int commonPrefix) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream((DataOutput)out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition() + commonPrefix, qlength - commonPrefix); } else { out.write(cell.getQualifierArray(), cell.getQualifierOffset() + commonPrefix, qlength - commonPrefix); } }
/** * Writes the qualifier from the given cell to the output stream * @param out The outputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param qlength the qualifier length * @throws IOException */ public static void writeQualifier(OutputStream out, Cell cell, int qlength) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils .copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition(), qlength); } else { out.write(cell.getQualifierArray(), cell.getQualifierOffset(), qlength); } }
/** * Writes the tag from the given cell to the output stream * @param out The outputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param tagsLength the tag length * @throws IOException */ public static void writeTags(OutputStream out, Cell cell, int tagsLength) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getTagsByteBuffer(), ((ByteBufferExtendedCell) cell).getTagsPosition(), tagsLength); } else { out.write(cell.getTagsArray(), cell.getTagsOffset(), tagsLength); } }
/** * Writes the row from the given cell to the output stream excluding the common prefix * @param out The dataoutputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param rlength the row length * @throws IOException */ public static void writeRowSkippingBytes(DataOutputStream out, Cell cell, short rlength, int commonPrefix) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils .copyBufferToStream((DataOutput) out, ((ByteBufferExtendedCell) cell).getRowByteBuffer(), ((ByteBufferExtendedCell) cell).getRowPosition() + commonPrefix, rlength - commonPrefix); } else { out.write(cell.getRowArray(), cell.getRowOffset() + commonPrefix, rlength - commonPrefix); } }
/** * Writes the qualifier from the given cell to the output stream excluding the common prefix * @param out The dataoutputstream to which the data has to be written * @param cell The cell whose contents has to be written * @param qlength the qualifier length * @throws IOException */ public static void writeQualifierSkippingBytes(DataOutputStream out, Cell cell, int qlength, int commonPrefix) throws IOException { if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyBufferToStream((DataOutput) out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition() + commonPrefix, qlength - commonPrefix); } else { out.write(cell.getQualifierArray(), cell.getQualifierOffset() + commonPrefix, qlength - commonPrefix); } }
/** * Test copying to stream from buffer with offset. * @throws IOException On test failure. */ @Test public void testCopyToStreamWithOffset() throws IOException { ByteBuffer buffer = ByteBuffer.wrap(array); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteBufferUtils.copyBufferToStream(bos, buffer, array.length / 2, array.length / 2); byte[] returnedArray = bos.toByteArray(); for (int i = 0; i < array.length / 2; ++i) { int pos = array.length / 2 + i; assertEquals(returnedArray[i], array[pos]); } }
ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getRowByteBuffer(), ((ByteBufferExtendedCell) cell).getRowPosition(), rowLen); out.writeByte(fLen); ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getFamilyByteBuffer(), ((ByteBufferExtendedCell) cell).getFamilyPosition(), fLen); ByteBufferUtils .copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition(), qLen); } else {
public static int writeFlatKey(Cell cell, OutputStream out) throws IOException { short rowLen = cell.getRowLength(); byte fLen = cell.getFamilyLength(); int qLen = cell.getQualifierLength(); // Using just one if/else loop instead of every time checking before writing every // component of cell if (cell instanceof ByteBufferExtendedCell) { StreamUtils.writeShort(out, rowLen); ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getRowByteBuffer(), ((ByteBufferExtendedCell) cell).getRowPosition(), rowLen); out.write(fLen); ByteBufferUtils.copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getFamilyByteBuffer(), ((ByteBufferExtendedCell) cell).getFamilyPosition(), fLen); ByteBufferUtils .copyBufferToStream(out, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition(), qLen); } else { StreamUtils.writeShort(out, rowLen); out.write(cell.getRowArray(), cell.getRowOffset(), rowLen); out.write(fLen); out.write(cell.getFamilyArray(), cell.getFamilyOffset(), fLen); out.write(cell.getQualifierArray(), cell.getQualifierOffset(), qLen); } StreamUtils.writeLong(out, cell.getTimestamp()); out.write(cell.getTypeByte()); return Bytes.SIZEOF_SHORT + rowLen + Bytes.SIZEOF_BYTE + fLen + qLen + Bytes.SIZEOF_LONG + Bytes.SIZEOF_BYTE; }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { ByteBufferUtils.copyBufferToStream(out, b, off, len); written += len; } }
/** * Copy the data to the output stream and update position in buffer. * @param out the stream to write bytes to * @param in the buffer to read bytes from * @param length the number of bytes to copy */ public static void moveBufferToStream(OutputStream out, ByteBuffer in, int length) throws IOException { copyBufferToStream(out, in, in.position(), length); skip(in, length); }
@Override public int write(OutputStream out, boolean withTags) throws IOException { int length = getSerializedSize(withTags); ByteBufferUtils.copyBufferToStream(out, this.buf, this.offset, length); return length; }
@Override public int write(OutputStream out, boolean withTags) throws IOException { int length = getSerializedSize(withTags); ByteBufferUtils.copyBufferToStream(out, this.buf, this.offset, length); return length; }