2013-02-08 14:15:19 +11:00
|
|
|
package com.wiredtiger.db;
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2013-02-08 14:15:19 +11:00
|
|
|
import java.lang.StringBuffer;
|
|
|
|
|
import com.wiredtiger.db.WiredTigerPackingException;
|
|
|
|
|
|
|
|
|
|
public class PackOutputStream {
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
final static int MAX_INT_BYTES = 21;
|
|
|
|
|
protected PackFormatInputStream format;
|
|
|
|
|
protected ByteArrayOutputStream packed;
|
|
|
|
|
protected byte[] intBuf;
|
2013-02-08 14:15:19 +11:00
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public PackOutputStream(String format) {
|
|
|
|
|
this.format = new PackFormatInputStream(format);
|
|
|
|
|
intBuf = new byte[MAX_INT_BYTES];
|
|
|
|
|
packed = new ByteArrayOutputStream(100);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getFormat() {
|
|
|
|
|
return format.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] getValue() {
|
2013-02-12 21:00:49 +11:00
|
|
|
return packed.toByteArray();
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-13 19:14:40 +11:00
|
|
|
public void reset() {
|
|
|
|
|
format.reset();
|
|
|
|
|
packed.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldByte(byte value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('b', true);
|
|
|
|
|
/* Translate to maintain ordering with the sign bit. */
|
|
|
|
|
byte input = (byte)(value + 0x80);
|
|
|
|
|
packed.write(input);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldByteArray(byte[] value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
this.addFieldByteArray(value, 0, value.length);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldByteArray(byte[] value, int off, int len)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('U', true);
|
|
|
|
|
// If this is not the last item, store the size.
|
|
|
|
|
if (format.available() > 0)
|
|
|
|
|
packLong(len, false);
|
2013-02-08 14:15:19 +11:00
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
packed.write(value, off, len);
|
2013-02-08 14:15:19 +11:00
|
|
|
/* TODO: padding. */
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldInt(int value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('i', true);
|
|
|
|
|
packLong(value, true);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldLong(long value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('q', true);
|
|
|
|
|
packLong(value, true);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldRecord(long value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('r', true);
|
|
|
|
|
packLong(value, true);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
public void addFieldShort(short value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('h', true);
|
|
|
|
|
packLong(value, true);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
//
|
|
|
|
|
// Strings have two possible encodings. A lower case 's' is not null
|
|
|
|
|
// terminated, and has a length define in the format (default 1). An
|
|
|
|
|
// upper case 'S' is variable length and has a null terminator.
|
|
|
|
|
public void addFieldString(String value)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
format.checkFieldType('s', false);
|
|
|
|
|
char fieldFormat = format.getFieldType();
|
|
|
|
|
int stringLen = 0;
|
|
|
|
|
int padBytes = 0;
|
|
|
|
|
if (fieldFormat == 's') {
|
|
|
|
|
stringLen = format.getLengthFromFormat(true);
|
|
|
|
|
if (stringLen > value.length())
|
|
|
|
|
padBytes = stringLen - value.length();
|
2013-02-08 14:15:19 +11:00
|
|
|
} else {
|
2013-02-12 21:00:49 +11:00
|
|
|
stringLen = value.length();
|
|
|
|
|
padBytes = 1; // Null terminator
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
2013-02-12 21:00:49 +11:00
|
|
|
// We're done pulling information from the field now.
|
2013-02-08 14:15:19 +11:00
|
|
|
format.consumeField();
|
|
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
// Use the default Charset.
|
|
|
|
|
packed.write(value.getBytes(), 0, stringLen);
|
|
|
|
|
while(padBytes-- > 0)
|
|
|
|
|
packed.write(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void packLong(long x, boolean signed)
|
|
|
|
|
throws WiredTigerPackingException {
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
|
|
if (!signed && x < 0)
|
|
|
|
|
throw new WiredTigerPackingException("Overflow packing long.");
|
|
|
|
|
|
|
|
|
|
if (x < PackUtil.NEG_2BYTE_MIN) {
|
|
|
|
|
intBuf[offset] = PackUtil.NEG_MULTI_MARKER;
|
|
|
|
|
int lz = Long.numberOfLeadingZeros(~x) / 8;
|
|
|
|
|
int len = PackUtil.SIZEOF_LONG - lz;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// There are four size bits we can use in the first
|
|
|
|
|
// byte. For negative numbers, we store the number of
|
|
|
|
|
// leading 0xff byes to maintain ordering (if this is
|
|
|
|
|
// not obvious, it may help to remember that -1 is the
|
|
|
|
|
// largest negative number).
|
|
|
|
|
intBuf[offset++] |= (lz & 0xf);
|
|
|
|
|
|
|
|
|
|
for (int shift = (len - 1) << 3;
|
|
|
|
|
len != 0; shift -= 8, --len)
|
|
|
|
|
intBuf[offset++] = (byte)(x >> shift);
|
|
|
|
|
} else if (x < PackUtil.NEG_1BYTE_MIN) {
|
|
|
|
|
x -= PackUtil.NEG_2BYTE_MIN;
|
|
|
|
|
intBuf[offset++] =
|
|
|
|
|
(byte)(PackUtil.NEG_2BYTE_MARKER | PackUtil.GET_BITS(x, 13, 8));
|
|
|
|
|
intBuf[offset++] = PackUtil.GET_BITS(x, 8, 0);
|
|
|
|
|
} else if (x < 0) {
|
|
|
|
|
x -= PackUtil.NEG_1BYTE_MIN;
|
|
|
|
|
intBuf[offset++] =
|
|
|
|
|
(byte)(PackUtil.NEG_1BYTE_MARKER | PackUtil.GET_BITS(x, 6, 0));
|
|
|
|
|
} else if (x <= PackUtil.POS_1BYTE_MAX) {
|
|
|
|
|
intBuf[offset++] =
|
|
|
|
|
(byte)(PackUtil.POS_1BYTE_MARKER | PackUtil.GET_BITS(x, 6, 0));
|
|
|
|
|
} else if (x <= PackUtil.POS_2BYTE_MAX) {
|
|
|
|
|
x -= PackUtil.POS_1BYTE_MAX + 1;
|
|
|
|
|
intBuf[offset++] =
|
|
|
|
|
(byte)(PackUtil.POS_2BYTE_MARKER | PackUtil.GET_BITS(x, 13, 8));
|
|
|
|
|
intBuf[offset++] = PackUtil.GET_BITS(x, 8, 0);
|
|
|
|
|
} else {
|
|
|
|
|
x -= PackUtil.POS_2BYTE_MAX + 1;
|
|
|
|
|
intBuf[offset] = PackUtil.POS_MULTI_MARKER;
|
|
|
|
|
int lz = Long.numberOfLeadingZeros(x) / 8;
|
|
|
|
|
int len = PackUtil.SIZEOF_LONG - lz;
|
2013-02-08 14:15:19 +11:00
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
// There are four bits we can use in the first byte.
|
|
|
|
|
intBuf[offset++] |= (len & 0xf);
|
2013-02-08 14:15:19 +11:00
|
|
|
|
2013-02-12 21:00:49 +11:00
|
|
|
for (int shift = (len - 1) << 3;
|
|
|
|
|
len != 0; --len, shift -= 8)
|
|
|
|
|
intBuf[offset++] = (byte)(x >> shift);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
2013-02-12 21:00:49 +11:00
|
|
|
packed.write(intBuf, 0, offset);
|
2013-02-08 14:15:19 +11:00
|
|
|
}
|
|
|
|
|
}
|