public static int encodeUTF8Length(int length, byte[] bytes, int start) { return VarLenIntEncoderDecoder.encode(length, bytes, start); }
public static int getNumBytesToStoreLength(int strlen) { return VarLenIntEncoderDecoder.getBytesRequired(strlen); }
public static int getUTFLength(byte[] b, int s) { return VarLenIntEncoderDecoder.decode(b, s); }
protected void testEncodeDecode(int expectedBytes, int value, byte[] bytes, int startPos) throws IOException { assertEquals(expectedBytes, encode(value, bytes, startPos)); assertEquals(value, decode(bytes, startPos)); ByteArrayInputStream bis = new ByteArrayInputStream(bytes, startPos, bytes.length - startPos); DataInputStream dis = new DataInputStream(bis); assertEquals(value, decode(dis)); } }
int actualMetaLen = VarLenIntEncoderDecoder.getBytesRequired(actualDataLength); if (actualMetaLen != estimateMetaLen) {// ugly but rare situation if the estimate vary a lot int diff = estimateMetaLen - actualMetaLen; VarLenIntEncoderDecoder.encode(actualDataLength, ary.getByteArray(), startOffset);
@Test public void testCreateDecoder() throws Exception { VarLenIntDecoder decoder = createDecoder(); byte[] bytes = new byte[100]; int pos = 1; for (int b : bounds) { pos += encode(b, bytes, pos); } decoder.reset(bytes, 1); for (int b : bounds) { assertEquals(b, decoder.decode()); } }
public static void serialize(byte[] instance, int start, int length, DataOutput out) throws HyracksDataException { byte[] metaBuffer = new byte[5]; int metaLength = VarLenIntEncoderDecoder.encode(length, metaBuffer, 0); try { out.write(metaBuffer, 0, metaLength); out.write(instance, start, length); } catch (IOException e) { throw HyracksDataException.create(e); } }
public static int getNumberBytesToStoreMeta(int length) { return VarLenIntEncoderDecoder.getBytesRequired(length); }
public static int getContentLength(byte[] bytes, int offset) { return VarLenIntEncoderDecoder.decode(bytes, offset); }
public static void writeUTF8(CharSequence str, DataOutput out, UTF8StringWriter writer) throws IOException { int strlen = str.length(); int utflen = 0; char c; int count = 0; for (int i = 0; i < strlen; i++) { c = str.charAt(i); utflen += UTF8StringUtil.getModifiedUTF8Len(c); } byte[] tempBytes = getTempBytes(writer, utflen); count += VarLenIntEncoderDecoder.encode(utflen, tempBytes, count); int i = 0; for (; i < strlen; i++) { c = str.charAt(i); if (!((c >= 0x0001) && (c <= 0x007F))) { break; } tempBytes[count++] = (byte) c; } for (; i < strlen; i++) { c = str.charAt(i); count += writeToBytes(tempBytes, count, c); } out.write(tempBytes, 0, count); }
@Override protected int getFieldSlotsBytes(ITupleReference tuple) { int fieldSlotBytes = 0; for (int i = inputKeyFieldCount; i < inputTotalFieldCount; i++) { if (!typeTraits[i].isFixedLength()) { fieldSlotBytes += VarLenIntEncoderDecoder.getBytesRequired(tuple.getFieldLength(i)); } } return fieldSlotBytes; }
public static byte[] read(DataInput in) throws HyracksDataException { try { int contentLength = VarLenIntEncoderDecoder.decode(in); byte[] bytes = new byte[contentLength]; in.readFully(bytes, 0, contentLength); return bytes; } catch (IOException e) { throw HyracksDataException.create(e); } }
public static ByteArrayPointable generatePointableFromPureBytes(byte[] bytes, int start, int length) { int metaLen = getNumberBytesToStoreMeta(length); byte[] ret = new byte[length + metaLen]; VarLenIntEncoderDecoder.encode(length, ret, 0); for (int i = 0; i < length; ++i) { ret[i + metaLen] = bytes[start + i]; } ByteArrayPointable ptr = new ByteArrayPointable(); ptr.set(ret, 0, ret.length); return ptr; }
protected int getFieldSlotsBytes(ITupleReference tuple, int startField, int numFields) { int fieldSlotBytes = 0; for (int i = startField; i < startField + numFields; i++) { if (!typeTraits[i].isFixedLength()) { fieldSlotBytes += VarLenIntEncoderDecoder.getBytesRequired(tuple.getFieldLength(i)); } } return fieldSlotBytes; }
public static String readUTF8(DataInput in, UTF8StringReader reader) throws IOException { int utflen = VarLenIntEncoderDecoder.decode(in); byte[] bytearr; char[] chararr;
public static void writeUTF8(char[] buffer, int start, int length, DataOutput out, UTF8StringWriter writer) throws IOException { int utflen = 0; int count = 0; char c; for (int i = 0; i < length; i++) { c = buffer[i + start]; utflen += UTF8StringUtil.getModifiedUTF8Len(c); } byte[] tempBytes = getTempBytes(writer, utflen); count += VarLenIntEncoderDecoder.encode(utflen, tempBytes, count); int i = 0; for (; i < length; i++) { c = buffer[i + start]; if (!((c >= 0x0001) && (c <= 0x007F))) { break; } tempBytes[count++] = (byte) c; } for (; i < length; i++) { c = buffer[i + start]; count += writeToBytes(tempBytes, count, c); } out.write(tempBytes, 0, count); }
/** * Start to build an variable length object * * @param ary the destination storage array * @param estimateLength the estimate length of this object * @throws IOException */ public void reset(GrowableArray ary, int estimateLength) throws IOException { this.ary = ary; this.out = ary.getDataOutput(); this.startOffset = ary.getLength(); this.estimateMetaLen = VarLenIntEncoderDecoder.getBytesRequired(estimateLength); // increase the offset for (int i = 0; i < estimateMetaLen; i++) { out.writeByte(0); } }
public int writeTupleFields(ITreeIndexTupleReference[] refs, int startField, ByteBuffer targetBuf, int targetOff) { int runner = targetOff; int nullFlagsBytes = getNullFlagsBytes(refs.length); // write null indicator bits for (int i = 0; i < nullFlagsBytes; i++) { targetBuf.put(runner++, (byte) 0); } // write field slots for variable length fields // since the r-tree has fixed length keys, we don't actually need this? for (int i = startField; i < startField + refs.length; i++) { if (!typeTraits[i].isFixedLength()) { runner += VarLenIntEncoderDecoder.encode(refs[i].getFieldLength(i), targetBuf.array(), runner); } } // write data for (int i = 0; i < refs.length; i++) { System.arraycopy(refs[i].getFieldData(i), refs[i].getFieldStart(i), targetBuf.array(), runner, refs[i].getFieldLength(i)); runner += refs[i].getFieldLength(i); } return runner - targetOff; } }
protected int getFieldSlotsBytes(ITupleReference tuple) { int fieldSlotBytes = 0; for (int i = 0; i < tuple.getFieldCount(); i++) { if (!typeTraits[i].isFixedLength()) { fieldSlotBytes += VarLenIntEncoderDecoder.getBytesRequired(tuple.getFieldLength(i)); } } return fieldSlotBytes; }
@Override public int writeTupleFields(ITupleReference tuple, int startField, int numFields, byte[] targetBuf, int targetOff) { int runner = targetOff; int nullFlagsBytes = getNullFlagsBytes(numFields); // write null indicator bits for (int i = 0; i < nullFlagsBytes; i++) { targetBuf[runner++] = (byte) 0; } // write field slots for variable length fields for (int i = startField; i < startField + numFields; i++) { if (!typeTraits[i].isFixedLength()) { runner += VarLenIntEncoderDecoder.encode(tuple.getFieldLength(i), targetBuf, runner); } } for (int i = startField; i < startField + numFields; i++) { System.arraycopy(tuple.getFieldData(i), tuple.getFieldStart(i), targetBuf, runner, tuple.getFieldLength(i)); runner += tuple.getFieldLength(i); } return runner - targetOff; }