GTSEncoder implements Cloneable { public byte[] toBlock(boolean compress) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(0); baos.write(0); baos.write(0); baos.write(0); byte[] payload = this.getBytes(); if (payload.length < 128) { compress = false; } baos.write(compress ? 1 : 0); baos.write(Varint.encodeSignedLong(this.baseTimestamp)); OutputStream out = baos; if (compress) { out = new GZIPOutputStream(out); } out.write(payload, 0, payload.length); out.flush(); out.close(); byte[] data = baos.toByteArray(); int len = data.length; data[0] = (byte) ((len >>> 24) & 0xff); data[1] = (byte) ((len >>> 16) & 0xff); data[2] = (byte) ((len >>> 8) & 0xff); data[3] = (byte) (len & 0xff); return data; } GTSEncoder();  GTSEncoder(long baseTimestamp, byte[] key, byte[] content);  GTSEncoder(long baseTimestamp);  GTSEncoder(long baseTimestamp, byte[] key);  GTSEncoder(long baseTimestamp, byte[] key, int size); synchronized int addValue(long timestamp, long location, long elevation, Object value); void setWrappingKey(byte[] key); byte[] getBytes(); int size(); long getCount(); synchronized void encode(GeoTimeSerie gts); synchronized void encodeOptimized(GeoTimeSerie gts); static Object optimizeValue(Object value); synchronized GTSDecoder getDecoder(boolean safeMetadata); GTSDecoder getDecoder(); synchronized GTSDecoder getUnsafeDecoder(boolean blockWrites); synchronized void reset(GTSEncoder encoder); synchronized void reset(long baseTS); synchronized void resize(int target); synchronized void merge(GTSEncoder encoder); long getBaseTimestamp(); long getClassId(); void setClassId(long classId); long getLabelsId(); void setLabelsId(long labelsId); String getName(); void setName(String name); Map<String, String> getLabels(); void setLabels(Map<String, String> labels); void setLabel(String key, String value); void setMetadata(Metadata metadata); void safeSetMetadata(Metadata metadata); Metadata getMetadata(); Metadata getRawMetadata(); long getLastTimestamp(); void safeDelta(); synchronized void setCount(long count); synchronized void flush(); byte[] toBlock(boolean compress); void writeTo(OutputStream out); GTSEncoder cloneEmpty(); GTSEncoder clone(); static final byte FLAGS_ENCRYPTED; }
@Test public void testToBlock() throws IOException { GTSEncoder encoder = new GTSEncoder(12345678); int n = 2; for (int i = 0; i < n; i++) { encoder.addValue(i * 1000, i * 12345678, i * 87654321, i * Math.PI); } System.out.println(encoder.size()); long nano = System.nanoTime(); byte[] block = encoder.toBlock(true); System.out.println((System.nanoTime() - nano) / 1000000.0D); System.out.println(block.length); nano = System.nanoTime(); GTSDecoder decoder = GTSDecoder.fromBlock(block, null); System.out.println((System.nanoTime() - nano) / 1000000.0D); GTSDecoder orig = encoder.getDecoder(); while(true) { boolean copy = decoder.next(); boolean org = orig.next(); Assert.assertEquals(copy,org); if (!copy) { break; } Assert.assertEquals(orig.getTimestamp(), decoder.getTimestamp()); Assert.assertEquals(orig.getLocation(), decoder.getLocation()); Assert.assertEquals(orig.getElevation(), decoder.getElevation()); Assert.assertEquals(orig.getValue(), decoder.getValue()); } }