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 into seqnos * <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 * @param seqnos the array to read the seqnos into, needs to have a length of 2 */ public static void readLongSequence(ByteBuffer buf, long seqnos[]) { byte len=buf.get(); if(len == 0) { seqnos[0]=seqnos[1]=0; return; } byte len1=firstNibble(len), len2=secondNibble(len); seqnos[0]=makeLong(buf, len1); seqnos[1]=makeLong(buf, len2) + seqnos[0]; }
case TYPE_CHAR: return (T)(Character)Bits.readChar(buffer, offset); case TYPE_DOUBLE: return (T)(Double)Bits.readDouble(buffer, offset); case TYPE_FLOAT: return (T)(Float)Bits.readFloat(buffer, offset); case TYPE_INT: return (T)(Integer)Bits.readInt(buffer, offset); case TYPE_LONG: return (T)(Long)Bits.readLong(buffer, offset); case TYPE_SHORT: return (T)(Short)Bits.readShort(buffer, offset); case TYPE_STRING: return (T)new String(buffer, offset, length); case TYPE_UTF_STRING:
public static long readLongCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; return makeLong(buf, offset, len); }
public static int readIntCompressed(byte[] buf, int offset) { byte len=buf[offset++]; if(len == 0) return 0; return makeInt(buf, offset, len); }
/** * 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 char readChar(byte[] buf, int offset) { return makeChar(buf, offset); }
case TYPE_CHAR: return (T)(Character)Bits.readChar(buffer, offset); case TYPE_DOUBLE: return (T)(Double)Bits.readDouble(buffer, offset); case TYPE_FLOAT: return (T)(Float)Bits.readFloat(buffer, offset); case TYPE_INT: return (T)(Integer)Bits.readInt(buffer, offset); case TYPE_LONG: return (T)(Long)Bits.readLong(buffer, offset); case TYPE_SHORT: return (T)(Short)Bits.readShort(buffer, offset); case TYPE_STRING: return (T)new String(buffer, offset, length); case TYPE_UTF_STRING:
/** * Reads a long from a buffer. * @param buf the buffer * @return the long read from the buffer */ public static long readLong(ByteBuffer buf) { byte len=buf.get(); if(len == 0) return 0; return makeLong(buf, len); }
/** * Reads an int from a buffer. * @param buf the buffer * @return the int read from the buffer */ public static int readInt(ByteBuffer buf) { byte len=buf.get(); if(len == 0) return 0; return makeInt(buf, len); }
/** * 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 char readChar(byte[] buf, int offset) { return makeChar(buf, offset); }
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); }
/** * Reads 2 compressed longs into an array of 2 longs. * <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 * @param seqnos the array to read the seqnos into, needs to have a length of 2 * @param index the index of the first element to be written; the seqnos are written to seqnos[index] and seqnos[index+1] */ public static void readLongSequence(DataInput in, long[] seqnos, int index) throws IOException { byte len=in.readByte(); if(len == 0) { seqnos[index]=seqnos[index+1]=0; return; } byte len1=firstNibble(len), len2=secondNibble(len); seqnos[index]=makeLong(in, len1); seqnos[index+1]=makeLong(in, len2) + seqnos[index]; }
/** * 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 a variable-length encoded long from an input stream. Note that currently variable-length encoding is <em>not</em> * used (a similar mechanism is used); this will be implemented later. * @param in the input stream * @return the long read from the input stream * @throws IOException */ public static long readLong(DataInput in) throws IOException { byte len=in.readByte(); if(len == 0) return 0; return makeLong(in, len); }
/** * Reads an int from an input stream * @param in the input stream * @return the int read from the input stream */ public static int readInt(DataInput in) throws IOException { byte len=in.readByte(); if(len == 0) return 0; return makeInt(in, 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); }
/** * Writes an int to an output stream * @param num the int to be written * @param out the output stream */ public static void writeInt(int num, DataOutput out) throws IOException { if(num == 0) { out.write(0); return; } final byte bytes_needed=bytesRequiredFor(num); out.write(bytes_needed); for(int i=0; i < bytes_needed; i++) out.write(getByteAt(num, i)); }