/** * @see #writeSignedVarLong(long, DataOutput) */ public static void writeSignedVarInt(int value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarInt((value << 1) ^ (value >> 31), out); }
@Override public int compare(byte[] left, byte[] right) { int l = Varint.readUnsignedVarInt(left); int r = Varint.readUnsignedVarInt(right); return l - r; } }
/** * @param in to read bytes from * @return decode value * @throws IOException if {@link DataInput} throws {@link IOException} * @throws IllegalArgumentException if variable-length value does not terminate * after 9 bytes have been read * @see #writeSignedVarLong(long, DataOutput) */ public static long readSignedVarLong(DataInput in) throws IOException { long raw = readUnsignedVarLong(in); // This undoes the trick in writeSignedVarLong() long temp = (((raw << 63) >> 63) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1L << 63)); }
output.writeShort((Short) value); } else if ((binding == Integer.class) || (binding == int.class)) { Varint.writeSignedVarInt((Integer) value, output); } else if ((binding == Long.class) || (binding == long.class)) { Varint.writeSignedVarLong((Long) value, output); } else if ((binding == Float.class) || (binding == float.class)) { output.writeFloat((Float) value); || (binding == java.sql.Timestamp.class) || (binding == java.util.Date.class)) { Varint.writeUnsignedVarLong(((Date) value).getTime(), output); } else if (Geometry.class.isAssignableFrom(binding)) { final TWKBWriter writer = new TWKBWriter(); final byte[] buffer = writer.write((Geometry) value); Varint.writeUnsignedVarInt(buffer.length, output); output.write(buffer); } else { oos.flush(); final byte[] bytes = bos.toByteArray(); Varint.writeUnsignedVarInt(bytes.length, output); output.write(bytes);
return input.readShort(); } else if ((binding == Integer.class) || (binding == int.class)) { return Varint.readSignedVarInt(input); } else if ((binding == Long.class) || (binding == long.class)) { return Varint.readSignedVarLong(input); } else if ((binding == Float.class) || (binding == float.class)) { return input.readFloat(); return input.readUTF(); } else if (binding == java.sql.Date.class) { return new java.sql.Date(Varint.readUnsignedVarLong(input)); } else if (binding == java.sql.Time.class) { return new java.sql.Time(Varint.readUnsignedVarLong(input)); } else if (binding == java.sql.Timestamp.class) { return new java.sql.Timestamp(Varint.readUnsignedVarLong(input)); } else if (binding == java.util.Date.class) { return new java.util.Date(Varint.readUnsignedVarLong(input)); } else if (Geometry.class.isAssignableFrom(binding)) { final TWKBReader reader = new TWKBReader(); final int length = Varint.readUnsignedVarInt(input); final byte[] buffer = new byte[length]; input.readFully(buffer); final int length = Varint.readUnsignedVarInt(input); final byte[] buffer = new byte[length]; input.readFully(buffer);
public void writePointArray(Coordinate[] coordinates, DataOutput output) throws IOException { long lastX = 0; long lastY = 0; Varint.writeUnsignedVarInt(coordinates.length, output); for (Coordinate c : coordinates) { long x = Math.round(c.getX() * precisionMultiplier); long y = Math.round(c.getY() * precisionMultiplier); Varint.writeSignedVarLong(x - lastX, output); Varint.writeSignedVarLong(y - lastY, output); lastX = x; lastY = y; } } }
public Coordinate[] readPointArray(DataInput input) throws IOException { int numCoordinates = Varint.readUnsignedVarInt(input); Coordinate[] coordinates = new Coordinate[numCoordinates]; long lastX = 0; long lastY = 0; for (int i = 0; i < numCoordinates; i++) { lastX = Varint.readSignedVarLong(input) + lastX; lastY = Varint.readSignedVarLong(input) + lastY; coordinates[i] = new Coordinate( ((double) lastX) / precisionMultiplier, ((double) lastY) / precisionMultiplier); } return coordinates; } }
visibility = new byte[0]; final byte[] value = new byte[Varint.readUnsignedVarInt(in)]; final int numDuplicates = in.readUnsignedByte(); if ((dataId.length > 0) && (in.read(dataId) != dataId.length)) { dataId, new GeoWaveValueImpl(fieldMask, visibility, value), Integer.toUnsignedLong(Varint.readSignedVarInt(in)), Varint.readSignedVarInt(in));
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> * Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently * encode signed values. If values are known to be nonnegative, * {@link #writeUnsignedVarLong(long, DataOutput)} should be used. * * @param value value to encode * @param out to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static void writeSignedVarLong(long value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarLong((value << 1) ^ (value >> 63), out); }
public Coordinate readPoint(DataInput input) throws IOException { return new Coordinate( ((double) Varint.readSignedVarLong(input)) / precisionMultiplier, ((double) Varint.readSignedVarLong(input)) / precisionMultiplier); }
@Override public ByteBuf encode(final Object in) throws IOException { if (in instanceof GeoWaveRedisPersistedTimestampRow) { final GeoWaveRedisPersistedTimestampRow row = (GeoWaveRedisPersistedTimestampRow) in; final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); try (final ByteBufOutputStream out = new ByteBufOutputStream(buf)) { GeoWaveRedisRowCodec.encodeRow(out, row, visibilityEnabled); Varint.writeSignedVarInt((int) row.getSecondsSinceEpic(), out); Varint.writeSignedVarInt(row.getNanoOfSecond(), out); out.flush(); return out.buffer(); } } throw new IOException("Encoder only supports GeoWaveRedisPersistedTimestampRow"); } };
@Override public void writePointArray(Coordinate[] coordinates, DataOutput output) throws IOException { long lastX = 0; long lastY = 0; long lastZ = 0; long lastM = 0; Varint.writeUnsignedVarInt(coordinates.length, output); for (Coordinate c : coordinates) { long x = Math.round(c.getX() * precisionMultiplier); long y = Math.round(c.getY() * precisionMultiplier); Varint.writeSignedVarLong(x - lastX, output); Varint.writeSignedVarLong(y - lastY, output); lastX = x; lastY = y; if (hasZ) { long z = Math.round(c.getZ() * zPrecisionMultiplier); Varint.writeSignedVarLong(z - lastZ, output); lastZ = z; } if (hasM) { long m = Math.round(c.getZ() * mPrecisionMultiplier); Varint.writeSignedVarLong(m - lastM, output); lastM = m; } } } }
@Override public Coordinate[] readPointArray(DataInput input) throws IOException { int numCoordinates = Varint.readUnsignedVarInt(input); Coordinate[] coordinates = new Coordinate[numCoordinates]; long lastX = 0; long lastY = 0; long lastZ = 0; long lastM = 0; for (int i = 0; i < numCoordinates; i++) { lastX = Varint.readSignedVarLong(input) + lastX; lastY = Varint.readSignedVarLong(input) + lastY; coordinates[i] = new Coordinate( ((double) lastX) / precisionMultiplier, ((double) lastY) / precisionMultiplier); if (hasZ) { lastZ = Varint.readSignedVarLong(input) + lastZ; coordinates[i].setZ(((double) lastZ) / zPrecisionMultiplier); } if (hasM) { lastM = Varint.readSignedVarLong(input) + lastM; coordinates[i].setM(((double) lastM) / mPrecisionMultiplier); } } return coordinates; } }
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> * Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently * encode signed values. If values are known to be nonnegative, * {@link #writeUnsignedVarLong(long, DataOutput)} should be used. * * @param value value to encode * @param out to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static void writeSignedVarLong(long value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarLong((value << 1) ^ (value >> 63), out); }
@Override public Coordinate readPoint(DataInput input) throws IOException { Coordinate coordinate = super.readPoint(input); if (hasZ) { coordinate.setZ(Varint.readSignedVarLong(input) / zPrecisionMultiplier); } if (hasM) { coordinate.setM(Varint.readSignedVarLong(input) / mPrecisionMultiplier); } return coordinate; }
public static byte[] writeSignedVarInt(int value) { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types return writeUnsignedVarInt((value << 1) ^ (value >> 31)); }
public static int readSignedVarInt(byte[] bytes) { int raw = readUnsignedVarInt(bytes); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * @param in to read bytes from * @return decode value * @throws IOException if {@link DataInput} throws {@link IOException} * @throws IllegalArgumentException if variable-length value does not terminate * after 9 bytes have been read * @see #writeSignedVarLong(long, DataOutput) */ public static long readSignedVarLong(DataInput in) throws IOException { long raw = readUnsignedVarLong(in); // This undoes the trick in writeSignedVarLong() long temp = (((raw << 63) >> 63) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1L << 63)); }
Varint.writeUnsignedVarInt(p, dos); Varint.writeUnsignedVarInt(sp, dos); if (format == Format.SPARSE) { mergeTempList(); Varint.writeUnsignedVarInt(0, dos); Varint.writeUnsignedVarInt(registerSet.size * 4, dos); for (int x : registerSet.readOnlyBits()) { dos.writeInt(x); Varint.writeUnsignedVarInt(1, dos); Varint.writeUnsignedVarInt(sparseSet.length, dos); int prevMergedDelta = 0; for (int k : sparseSet) { Varint.writeUnsignedVarInt(k - prevMergedDelta, dos); prevMergedDelta = k;
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readSignedVarLong(DataInput) */ public static int readSignedVarInt(DataInput in) throws IOException { int raw = readUnsignedVarInt(in); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }