private static void writeUnsigned(WriteBuffer out, int offset, final long value) { assert offset % 7 == 0; while (offset > 0) { offset -= 7; byte b = (byte) ((value >>> offset) & BIT_MASK); if (offset == 0) { b = (byte) (b | STOP_MASK); } out.putByte(b); } }
byte b = (byte)c; if (i+1==attribute.length()) b |= 0x80; //End marker buffer.putByte(b); int c = attribute.charAt(i); if (c <= 0x007F) { buffer.putByte((byte)c); } else if (c > 0x07FF) { buffer.putByte((byte)(0xE0 | c >> 12 & 0x0F)); buffer.putByte((byte)(0x80 | c >> 6 & 0x3F)); buffer.putByte((byte)(0x80 | c & 0x3F)); } else { buffer.putByte((byte)(0xC0 | c >> 6 & 0x1F)); buffer.putByte((byte)(0x80 | c & 0x3F));
public static WriteBuffer put(WriteBuffer out, byte[] bytes) { for (byte aByte : bytes) out.putByte(aByte); return out; }
@Override public void write(WriteBuffer out, Byte object) { out.putByte((byte)(object - Byte.MIN_VALUE)); }
@Override public void write(WriteBuffer buffer, Object attribute) { buffer.putByte((byte)1); }
@Override public void write(WriteBuffer buffer, byte[] attribute) { writeLength(buffer,attribute); if (attribute!=null) for (byte anAttribute : attribute) buffer.putByte(anAttribute); } }
@Override public void write(WriteBuffer out, Boolean attribute) { out.putByte(encode(attribute)); }
@Override public void writeByteOrder(WriteBuffer buffer, String attribute) { if (attribute==null) { buffer.putByte((byte)-1); return; } else { buffer.putByte((byte)0); } for (int i = 0; i < attribute.length(); i++) { char c = attribute.charAt(i); Preconditions.checkArgument(((int) c) > 0, "No null characters allowed in string @ position %s: %s", i, attribute); cs.writeByteOrder(buffer, c); } cs.writeByteOrder(buffer, (char) 0); }
@Override public void write(WriteBuffer buffer, boolean[] attribute) { writeLength(buffer,attribute); if (attribute==null) return; byte b = 0; int i = 0; for (; i < attribute.length; i++) { b = (byte)( ((int)b<<1) | BooleanSerializer.encode(attribute[i])); if ((i+1)%8 == 0) { buffer.putByte(b); b=0; } } if (i%8!=0) buffer.putByte((byte)(b<<(8-(i%8)))); } }
/** * The format used is this: * - The first bit indicates whether this is the first block (reading backwards, this would be the stop criterion) * - In the first byte, the 3 bits after the first bit indicate the number of bytes written minus 3 (since 3 is * the minimum number of bytes written. So, if the 3 bits are 010 = 2 => 5 bytes written. The value is aligned to * the left to ensure that this encoding is byte order preserving. * * @param out * @param value */ private static void writeUnsignedBackward(WriteBuffer out, final long value) { int numBytes = unsignedBackwardLength(value); int prefixLen = numBytes - 3; assert prefixLen >= 0 && prefixLen < 8; //Consumes 3 bits //Prepare first byte byte b = (byte)((prefixLen << 4) | 0x80); //stop marker (first bit) and length for (int i = numBytes - 1; i >= 0; i--) { b = (byte)(b | (0x7F & (value >>> (i * 7)))); out.putByte(b); b = 0; } }
public static WriteBuffer put(WriteBuffer out, StaticBuffer in) { for (int i=0;i<in.length();i++) out.putByte(in.getByte(i)); return out; }
public static void writePositiveWithPrefix(final WriteBuffer out, long value, long prefix, final int prefixBitLen) { assert value >= 0; assert prefixBitLen > 0 && prefixBitLen < 6 && (prefix < (1L << prefixBitLen)); //Write first byte int deltaLen = 8 - prefixBitLen; byte first = (byte)(prefix << deltaLen); int valueLen = unsignedBitLength(value); int mod = valueLen % 7; if (mod <= (deltaLen - 1)) { int offset = (valueLen - mod); first = (byte)(first | (value >>> offset)); value = value & ((1L << offset) - 1); valueLen -= mod; } else { valueLen += (7 - mod); } assert valueLen >= 0; if (valueLen > 0) { //Add continue mask to indicate reading further first = (byte) ( first | (1 << (deltaLen - 1))); } out.putByte(first); if (valueLen > 0) { //Keep writing writeUnsigned(out, valueLen, value); } }