1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
6import java.math.BigInteger;
8public abstract class BinarySerializer implements Serializer {
9 protected MyByteArrayOutputStream output;
10 private long containerDepthBudget;
12 public BinarySerializer(long maxContainerDepth) {
13 output = new BinarySerializer.MyByteArrayOutputStream();
14 containerDepthBudget = maxContainerDepth;
17 public void increase_container_depth() throws SerializationError {
18 if (containerDepthBudget == 0) {
19 throw new SerializationError("Exceeded maximum container depth");
21 containerDepthBudget -= 1;
24 public void decrease_container_depth() {
25 containerDepthBudget += 1;
28 public void serialize_str(String value) throws SerializationError {
29 serialize_bytes(new Bytes(value.getBytes()));
32 public void serialize_bytes(Bytes value) throws SerializationError {
33 byte[] content = value.content();
34 serialize_len(content.length);
35 output.write(content, 0, content.length);
38 public void serialize_bool(Boolean value) throws SerializationError {
39 output.write((value.booleanValue() ? 1 : 0));
42 public void serialize_unit(Unit value) throws SerializationError {
45 public void serialize_char(Character value) throws SerializationError {
46 throw new SerializationError("Not implemented: serialize_char");
49 public void serialize_u8(@Unsigned Byte value) throws SerializationError {
50 output.write(value.byteValue());
53 public void serialize_u16(@Unsigned Short value) throws SerializationError {
54 short val = value.shortValue();
55 output.write((byte) (val >>> 0));
56 output.write((byte) (val >>> 8));
59 public void serialize_u32(@Unsigned Integer value) throws SerializationError {
60 int val = value.intValue();
61 output.write((byte) (val >>> 0));
62 output.write((byte) (val >>> 8));
63 output.write((byte) (val >>> 16));
64 output.write((byte) (val >>> 24));
67 public void serialize_u64(@Unsigned Long value) throws SerializationError {
68 long val = value.longValue();
69 output.write((byte) (val >>> 0));
70 output.write((byte) (val >>> 8));
71 output.write((byte) (val >>> 16));
72 output.write((byte) (val >>> 24));
73 output.write((byte) (val >>> 32));
74 output.write((byte) (val >>> 40));
75 output.write((byte) (val >>> 48));
76 output.write((byte) (val >>> 56));
79 public void serialize_u128(@Unsigned @Int128 BigInteger value) throws SerializationError {
80 if (value.compareTo(BigInteger.ZERO) < 0 || !value.shiftRight(128).equals(BigInteger.ZERO)) {
81 throw new java.lang.IllegalArgumentException("Invalid value for an unsigned int128");
83 byte[] content = value.toByteArray();
84 // BigInteger.toByteArray() may add a most-significant zero
85 // byte for signing purpose: ignore it.
86 assert content.length <= 16 || content[0] == 0;
87 int len = Math.min(content.length, 16);
88 // Write content in little-endian order.
89 for (int i = 0; i < len; i++) {
90 output.write(content[content.length - 1 - i]);
92 // Complete with zeros if needed.
93 for (int i = len; i < 16; i++) {
98 public void serialize_i8(Byte value) throws SerializationError {
102 public void serialize_i16(Short value) throws SerializationError {
103 serialize_u16(value);
106 public void serialize_i32(Integer value) throws SerializationError {
107 serialize_u32(value);
110 public void serialize_i64(Long value) throws SerializationError {
111 serialize_u64(value);
114 public void serialize_i128(@Int128 BigInteger value) throws SerializationError {
115 if (value.compareTo(BigInteger.ZERO) >= 0) {
116 if (!value.shiftRight(127).equals(BigInteger.ZERO)) {
117 throw new java.lang.IllegalArgumentException("Invalid value for a signed int128");
119 serialize_u128(value);
121 if (!value.add(BigInteger.ONE).negate().shiftRight(127).equals(BigInteger.ZERO)) {
122 throw new java.lang.IllegalArgumentException("Invalid value for a signed int128");
124 serialize_u128(value.add(BigInteger.ONE.shiftLeft(128)));
128 public void serialize_option_tag(boolean value) throws SerializationError {
129 output.write((value ? (byte) 1 : (byte) 0));
132 public int get_buffer_offset() {
133 return output.size();
136 public byte[] get_bytes() {
137 return output.toByteArray();
140 // Local extension to provide access to the underlying buffer.
141 static public class MyByteArrayOutputStream extends java.io.ByteArrayOutputStream {
142 public byte[] getBuffer() {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list