public static int encodeUTF8Length(int length, byte[] bytes, int start) { return VarLenIntEncoderDecoder.encode(length, bytes, start); }
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 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); }
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; }
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); }
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; } }
@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; }
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 int writeTuple(ITupleReference tuple, byte[] targetBuf, int targetOff) { int runner = targetOff; int nullFlagsBytes = getNullFlagsBytes(tuple); // 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 = 0; i < tuple.getFieldCount(); i++) { if (!typeTraits[i].isFixedLength()) { runner += VarLenIntEncoderDecoder.encode(tuple.getFieldLength(i), targetBuf, runner); } } // write data fields for (int i = 0; i < tuple.getFieldCount(); i++) { System.arraycopy(tuple.getFieldData(i), tuple.getFieldStart(i), targetBuf, runner, tuple.getFieldLength(i)); runner += tuple.getFieldLength(i); } return runner - targetOff; }
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()); } }
runner += VarLenIntEncoderDecoder.encode(tuple.getFieldLength(i), targetBuf, runner);
int metaLength = VarLenIntEncoderDecoder.encode(subLength, metaBuffer, 0); dataOutput.write(metaBuffer, 0, metaLength);