public static int getContentLength(byte[] bytes, int offset) { return VarLenIntEncoderDecoder.decode(bytes, offset); }
public static VarLenIntDecoder createDecoder() { return new VarLenIntDecoder(); }
public static int encodeUTF8Length(int length, byte[] bytes, int start) { return VarLenIntEncoderDecoder.encode(length, bytes, start); }
@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 int getNumBytesToStoreLength(int strlen) { return VarLenIntEncoderDecoder.getBytesRequired(strlen); }
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)); } }
@Override public void resetByTupleOffset(byte[] buf, int tupleStartOff) { this.buf = buf; this.tupleStartOff = tupleStartOff; // decode field slots int field = 0; int cumul = 0; int end = fieldStartIndex + fieldCount; encDec.reset(buf, tupleStartOff + nullFlagsBytes); for (int i = fieldStartIndex; i < end; i++) { if (!typeTraits[i].isFixedLength()) { cumul += encDec.decode(); decodedFieldSlots[field++] = cumul; } else { cumul += typeTraits[i].getFixedLength(); decodedFieldSlots[field++] = cumul; } } dataStartOff = encDec.getPos(); }
@Test public void testEncodeDecode() throws Exception { byte[] bytes = new byte[10]; int startPos = 3; for (int i = 1; i < bounds.length - 1; i++) { testEncodeDecode(i, bounds[i] - 1, bytes, startPos); testEncodeDecode(i + 1, bounds[i], bytes, startPos); testEncodeDecode(i + 1, bounds[i] + 1, bytes, startPos); } // Integer.Max testEncodeDecode(5, BOUND_FIVE_BYTE, bytes, startPos); }
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 getUTFLength(byte[] b, int s) { return VarLenIntEncoderDecoder.decode(b, s); }
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; }
@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 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); }
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; }
/** * 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); } }
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; }
@Test public void testGetBytesRequired() throws Exception { for (int bound = 0; bound < bounds.length - 1; bound++) { assertEquals(bound + 1, getBytesRequired(bounds[bound])); assertEquals(bound + 1, getBytesRequired(bounds[bound + 1] - 1)); } }