public static void writeIntCompressed(int num, byte[] buf, int offset) { if(num == 0) { buf[offset]=0; return; } final byte bytes_needed=bytesRequiredFor(num); buf[offset++]=bytes_needed; for(int i=0; i < bytes_needed; i++) buf[offset++]=getByteAt(num, i); }
public static char readChar(byte[] buf, int offset) { return makeChar(buf, offset); }
public static int readIntCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; byte[] buffer=new byte[len]; for(int i=0; i < len; i++) buffer[i]=buf[offset++]; return makeInt(buffer, 0, len); }
case TYPE_CHAR: return (Character)Bits.readChar(buffer, offset); case TYPE_DOUBLE: return (Double)Bits.readDouble(buffer, offset); case TYPE_FLOAT: return (Float)Bits.readFloat(buffer, offset); case TYPE_INT: return (Integer)Bits.readInt(buffer, offset); case TYPE_LONG: return (Long)Bits.readLong(buffer, offset); case TYPE_SHORT: return (Short)Bits.readShort(buffer, offset); case TYPE_STRING: return new String(buffer, offset, length); case TYPE_UTF_STRING:
/** * Writes 2 sequence numbers (seqnos) in compressed format to an output stream. * The seqnos are non-negative and hr is guaranteed to be >= hd. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * write the 2 longs individually. * @param hd the highest delivered seqno. Guaranteed to be a positive number * @param hr the highest received seqno. Guaranteed to be a positive number. Greater than or equal to hd * @param out the output stream to write to */ public static void writeLongSequence(long hd, long hr, DataOutput out) throws IOException { if(hr < hd) throw new IllegalArgumentException("hr (" + hr + ") has to be >= hd (" + hd + ")"); if(hd == 0 && hr == 0) { out.write(0); return; } long delta=hr - hd; // encode highest_delivered followed by delta byte bytes_for_hd=bytesRequiredFor(hd), bytes_for_delta=bytesRequiredFor(delta); byte bytes_needed=encodeLength(bytes_for_hd, bytes_for_delta); out.write(bytes_needed); for(int i=0; i < bytes_for_hd; i++) out.write(getByteAt(hd, i)); for(int i=0; i < bytes_for_delta; i++) out.write(getByteAt(delta, i)); }
/** * Reads 2 compressed longs from in. * Reads 2 compressed longs from buf. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * read the 2 longs individually. * @param in the input stream to read from * @return an array of 2 longs (hd and hr) */ public static long[] readLongSequence(DataInput in) throws IOException { byte len=in.readByte(); if(len == 0) return new long[]{0,0}; byte[] lengths=decodeLength(len); long[] seqnos=new long[2]; byte[] buf=new byte[lengths[0] + lengths[1]]; in.readFully(buf, 0, buf.length); seqnos[0]=makeLong(buf, 0, lengths[0]); seqnos[1]=makeLong(buf, lengths[0], lengths[1]) + seqnos[0]; return seqnos; }
/** * Computes the size of a variable-length encoded int * @param num the int * @return the number of bytes needed to variable-length encode num */ public static int size(int num) { return (byte)(num == 0? 1 : bytesRequiredFor(num) +1); }
public static long readLongCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; byte[] buffer=new byte[len]; for(int i=0; i < len; i++) buffer[i]=buf[offset++]; return makeLong(buffer, 0, len); }
case TYPE_CHAR: return (Character)Bits.readChar(buffer, offset); case TYPE_DOUBLE: return (Double)Bits.readDouble(buffer, offset); case TYPE_FLOAT: return (Float)Bits.readFloat(buffer, offset); case TYPE_INT: return (Integer)Bits.readInt(buffer, offset); case TYPE_LONG: return (Long)Bits.readLong(buffer, offset); case TYPE_SHORT: return (Short)Bits.readShort(buffer, offset); case TYPE_STRING: return new String(buffer, offset, length); case TYPE_UTF_STRING:
/** * Writes 2 sequence numbers (seqnos) in compressed format to an output stream. * The seqnos are non-negative and hr is guaranteed to be >= hd. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * write the 2 longs individually. * @param hd the highest delivered seqno. Guaranteed to be a positive number * @param hr the highest received seqno. Guaranteed to be a positive number. Greater than or equal to hd * @param out the output stream to write to */ public static void writeLongSequence(long hd, long hr, DataOutput out) throws IOException { if(hr < hd) throw new IllegalArgumentException("hr (" + hr + ") has to be >= hd (" + hd + ")"); if(hd == 0 && hr == 0) { out.write(0); return; } long delta=hr - hd; // encode highest_delivered followed by delta byte bytes_for_hd=bytesRequiredFor(hd), bytes_for_delta=bytesRequiredFor(delta); byte bytes_needed=encodeLength(bytes_for_hd, bytes_for_delta); out.write(bytes_needed); for(int i=0; i < bytes_for_hd; i++) out.write(getByteAt(hd, i)); for(int i=0; i < bytes_for_delta; i++) out.write(getByteAt(delta, i)); }
/** * Reads 2 compressed longs from buf. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * read the 2 longs individually. * @param buf the buffer to read from * @return an array of 2 longs (hd and hr) */ public static long[] readLongSequence(ByteBuffer buf) { byte len=buf.get(); if(len == 0) return new long[]{0,0}; byte[] lengths=decodeLength(len); long[] seqnos=new long[2]; byte[] retval=new byte[lengths[0] + lengths[1]]; buf.get(retval, 0, retval.length); seqnos[0]=makeLong(retval, 0, lengths[0]); seqnos[1]=makeLong(retval, lengths[0], lengths[1]) + seqnos[0]; return seqnos; }
/** * Computes the size of a variable-length encoded long. Note that this is <em>not</em> currently using * variable-length encoding (will be implemented later). * @param num the long * @return the number of bytes needed to variable-length encode num */ public static int size(long num) { return (byte)(num == 0? 1 : bytesRequiredFor(num) +1); }
public static long readLongCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; byte[] buffer=new byte[len]; for(int i=0; i < len; i++) buffer[i]=buf[offset++]; return makeLong(buffer, 0, len); }
case TYPE_CHAR: return (Character)Bits.readChar(buffer, offset); case TYPE_DOUBLE: return (Double)Bits.readDouble(buffer, offset); case TYPE_FLOAT: return (Float)Bits.readFloat(buffer, offset); case TYPE_INT: return (Integer)Bits.readInt(buffer, offset); case TYPE_LONG: return (Long)Bits.readLong(buffer, offset); case TYPE_SHORT: return (Short)Bits.readShort(buffer, offset); case TYPE_STRING: return new String(buffer, offset, length); case TYPE_UTF_STRING:
public static void writeLongCompressed(long num, byte[] buf, int offset) { if(num == 0) { buf[offset]=0; return; } final byte bytes_needed=bytesRequiredFor(num); buf[offset++]=bytes_needed; for(int i=0; i < bytes_needed; i++) buf[offset++]=getByteAt(num, i); }
/** * Writes 2 sequence numbers (seqnos) in compressed format to buf. * The seqnos are non-negative and hr is guaranteed to be >= hd. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * write the 2 longs individually. * @param hd the highest delivered seqno. Guaranteed to be a positive number * @param hr the highest received seqno. Guaranteed to be a positive number. Greater than or equal to hd * @param buf the buffer to write to */ public static void writeLongSequence(long hd, long hr, ByteBuffer buf) { if(hr < hd) throw new IllegalArgumentException("hr (" + hr + ") has to be >= hd (" + hd + ")"); if(hd == 0 && hr == 0) { buf.put((byte)0); return; } long delta=hr - hd; // encode highest_delivered followed by delta byte bytes_for_hd=bytesRequiredFor(hd), bytes_for_delta=bytesRequiredFor(delta); byte bytes_needed=encodeLength(bytes_for_hd, bytes_for_delta); buf.put(bytes_needed); for(int i=0; i < bytes_for_hd; i++) buf.put(getByteAt(hd, i)); for(int i=0; i < bytes_for_delta; i++) buf.put(getByteAt(delta, i)); }
/** * Reads 2 compressed longs from buf. * <p/> * Once variable-length encoding has been implemented, this method will probably get dropped as we can simply * read the 2 longs individually. * @param buf the buffer to read from * @return an array of 2 longs (hd and hr) */ public static long[] readLongSequence(ByteBuffer buf) { byte len=buf.get(); if(len == 0) return new long[]{0,0}; byte[] lengths=decodeLength(len); long[] seqnos=new long[2]; byte[] retval=new byte[lengths[0] + lengths[1]]; buf.get(retval, 0, retval.length); seqnos[0]=makeLong(retval, 0, lengths[0]); seqnos[1]=makeLong(retval, lengths[0], lengths[1]) + seqnos[0]; return seqnos; }
public static int readIntCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; byte[] buffer=new byte[len]; for(int i=0; i < len; i++) buffer[i]=buf[offset++]; return makeInt(buffer, 0, len); }
public static byte size(long hd, long hr) { if(hd == 0 && hr == 0) return 1; byte num_bytes_for_hd=bytesRequiredFor(hd), num_bytes_for_delta=bytesRequiredFor(hr - hd); return (byte)(num_bytes_for_hd + num_bytes_for_delta + 1); }
public static long readLongCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; byte[] buffer=new byte[len]; for(int i=0; i < len; i++) buffer[i]=buf[offset++]; return makeLong(buffer, 0, len); }