diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java |
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java b/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java new file mode 100644 index 00000000000..4a126932f1e --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java @@ -0,0 +1,88 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.slime; + +/** + * Class for serializing Slime data into binary format, or deserializing + * the binary format into a Slime object. + **/ +public class BinaryFormat { + static long encode_zigzag(long x) { + return ((x << 1) ^ (x >> 63)); // note ASR + } + + static long decode_zigzag(long x) { + return ((x >>> 1) ^ (-(x & 0x1))); // note LSR + } + + static long encode_double(double x) { + return Double.doubleToRawLongBits(x); + } + + static double decode_double(long x) { + return Double.longBitsToDouble(x); + } + + static byte encode_type_and_meta(int type, int meta) { + return (byte) ((meta << 3) | (type & 0x7)); + } + + static Type decode_type(byte type_and_meta) { + return Type.asType(type_and_meta & 0x7); + } + + static int decode_meta(byte type_and_meta) { + return ((type_and_meta & 0xff) >>> 3); + } + + /** + * Take a Slime object and serialize it into binary format. + * @param slime the object which is to be serialized. + * @return a new byte array with just the encoded slime. + **/ + public static byte[] encode(Slime slime) { + BinaryEncoder encoder = new BinaryEncoder(); + return encoder.encode(slime); + } + + /** + * Take binary data and deserialize it into a Slime object. + * The data is assumed to be the binary representation + * as if obtained by a call to the @ref encode() method. + * + * If the binary data can't be deserialized without problems + * the returned Slime object will instead only contain the + * three fields "partial_result" (contains anything successfully + * decoded before encountering problems), "offending_input" + * (containing any data that could not be deserialized) and + * "error_message" (a string describing the problem encountered). + * + * @param data the data to be deserialized. + * @return a new Slime object constructed from the data. + **/ + public static Slime decode(byte[] data) { + BinaryDecoder decoder = new BinaryDecoder(); + return decoder.decode(data); + } + + /** + * Take binary data and deserialize it into a Slime object. + * The data is assumed to be the binary representation + * as if obtained by a call to the @ref encode() method. + * + * If the binary data can't be deserialized without problems + * the returned Slime object will instead only contain the + * three fields "partial_result" (contains anything successfully + * decoded before encountering problems), "offending_input" + * (containing any data that could not be deserialized) and + * "error_message" (a string describing the problem encountered). + * + * @param data array containing the data to be deserialized. + * @param offset where in the array to start deserializing. + * @param length how many bytes the deserializer is allowed to consume. + * @return a new Slime object constructed from the data. + **/ + public static Slime decode(byte[] data, int offset, int length) { + BinaryDecoder decoder = new BinaryDecoder(); + return decoder.decode(data, offset, length); + } +} |