From 66c3a0f3153db6cce1390577ab43509a0479ef43 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Sat, 29 Jun 2024 15:00:57 +0200 Subject: [PATCH] The CircularByteBuffer seems to be correct now. Added Cbor lib for possible future use --- .../exceptional/CircularByteBuffer.java | 247 ++++++--- .../exceptional/ExceptionLogger.java | 4 + .../shautvast/exceptional/RingBuffer.java | 179 +++++++ lib/src/main/java/jacob/CborConstants.java | 89 ++++ lib/src/main/java/jacob/CborDecoder.java | 498 ++++++++++++++++++ lib/src/main/java/jacob/CborEncoder.java | 488 +++++++++++++++++ lib/src/main/java/jacob/CborType.java | 142 +++++ .../exceptional/CircularByteBufferTest.java | 63 +-- .../shautvast/exceptional/RingBufferTest.java | 24 + 9 files changed, 1633 insertions(+), 101 deletions(-) create mode 100644 lib/src/main/java/com/github/shautvast/exceptional/RingBuffer.java create mode 100644 lib/src/main/java/jacob/CborConstants.java create mode 100644 lib/src/main/java/jacob/CborDecoder.java create mode 100644 lib/src/main/java/jacob/CborEncoder.java create mode 100644 lib/src/main/java/jacob/CborType.java create mode 100644 lib/src/test/java/com/github/shautvast/exceptional/RingBufferTest.java diff --git a/lib/src/main/java/com/github/shautvast/exceptional/CircularByteBuffer.java b/lib/src/main/java/com/github/shautvast/exceptional/CircularByteBuffer.java index 7030046..70407df 100644 --- a/lib/src/main/java/com/github/shautvast/exceptional/CircularByteBuffer.java +++ b/lib/src/main/java/com/github/shautvast/exceptional/CircularByteBuffer.java @@ -1,107 +1,212 @@ package com.github.shautvast.exceptional; +import java.lang.foreign.MemorySegment; import java.nio.ByteBuffer; import java.util.stream.Collectors; import java.util.stream.IntStream; /** - * Circular buffer for variable sized byte arrays - * The singlethread version + * Circular buffer for variable sized byte arrays. The indices for read and write + * are also stored in the bytebuffer, making changes visible to any non-java process that is reading. + * + * Written for a scenario with multiple concurrent writers, and a single reader in a non-java process + * This class itself is Not Threadsafe! It relies on MPSCBufferWriter for multithreaded writes. This queues + * byte arrays waiting to be stored in the circular buffer. MPSCBufferWriter starts the only + * thread that is allowed to interact with the CircularByteBuffer. + * .. + * *Implementation note:* + * The first 8 bytes are reserved for the reader and writer index. START constant indicates the actual startindex + * of the payload data. The index stored is the actual index (ie starting at 8). The reads and write methods + * for reader/writer index deal with the offset value, so that the index (as method local variable) does not + * include it (ie starting at 0). This simplifies the calculations that include these indices. Same goes for the + * capacity. */ @SuppressWarnings("StringTemplateMigration") public class CircularByteBuffer { + public static final int READ = 0; + public static final int WRITE = 4; + public static final int START = 8; final ByteBuffer data; - int readIndex = 0; - int writeIndex = 0; + /** + * Constructs a CircularByteBuffer with the specified capacity. + * The buffer is backed by a byte array on the java-heap. Mainly there for test purposes. + * + * @param capacity the capacity of the CircularByteBuffer + */ public CircularByteBuffer(int capacity) { - data = ByteBuffer.allocate(capacity); + data = ByteBuffer.allocate(capacity + START); // 8 extra for the read and write index + data.putInt(READ, START); + data.putInt(WRITE, START); + } + + /** + * Constructs a CircularByteBuffer with the specified capacity. The buffer is backed by native memory + * from the MemorySegment + */ + public CircularByteBuffer(MemorySegment memory) { + if (memory.byteSize() > 0xfff7) { + throw new IllegalArgumentException("Max memory size is 65527"); + } + this.data = memory.asByteBuffer(); } public boolean put(byte[] bytes) { int len = bytes.length; int remaining; // check capacity for bytes to insert - if (writeIndex >= readIndex) { - remaining = data.capacity() - writeIndex + readIndex; - } else { - remaining = readIndex - writeIndex; - } - if (remaining < len + 2) { - return false; - } else { - int remainingUntilEnd = data.capacity() - writeIndex; - if (remainingUntilEnd < len + 2) { - if (remainingUntilEnd > 1) { - // we can write the length - data.putShort(writeIndex, (short) len); - writeIndex += 2; - remainingUntilEnd -= 2; - if (remainingUntilEnd > 0) { - data.put(writeIndex, bytes, 0, remainingUntilEnd); - } - writeIndex = 0; - data.put(writeIndex, bytes, remainingUntilEnd, len - remainingUntilEnd); - writeIndex += len - remainingUntilEnd; - } else { - // we can write only one byte of the length - data.put(writeIndex, (byte) (len >> 8)); - writeIndex = 0; - data.put(writeIndex, (byte) (len & 0xff)); - writeIndex += 1; - data.put(writeIndex, bytes); - writeIndex += len; - } + int readIndex = getReadIndex(); + int writeIndex = getWriteIndex(); + try { + if (writeIndex >= readIndex) { + remaining = capacity() - writeIndex + readIndex; } else { - data.putShort(writeIndex, (short) len); - writeIndex += 2; - data.put(writeIndex, bytes); - writeIndex += len; + remaining = readIndex - writeIndex; } - return true; + if (remaining < len + 2) { + return false; + } else { + int remainingUntilEnd = capacity() - writeIndex; + if (remainingUntilEnd < len + 2) { + if (remainingUntilEnd > 1) { + // we can write the length + putShort(writeIndex, (short) len); + writeIndex += 2; + remainingUntilEnd -= 2; + if (remainingUntilEnd > 0) { + put(writeIndex, bytes, 0, remainingUntilEnd); + } + writeIndex = 0; + put(writeIndex, bytes, remainingUntilEnd, len); + writeIndex += len - remainingUntilEnd; + } else { + // we can write only one byte of the length + put(writeIndex, (byte) (len >> 8)); + writeIndex = 0; + put(writeIndex, (byte) (len & 0xff)); + writeIndex += 1; + put(writeIndex, bytes); + writeIndex += len; + } + } else { + putShort(writeIndex, (short) len); + writeIndex += 2; + put(writeIndex, bytes); + writeIndex += len; + + if (writeIndex == capacity()) { + writeIndex = 0; + } + } + + return true; + } + } finally { + setWriteIndex(writeIndex); } } + private int capacity() { + return data.capacity() - START; + } + + /** + * The reader side is provided, for reference and testability only. + * In practice, the reader is implemented outside of java + */ public byte[] get() { - int remainingUntilEnd = data.capacity() - readIndex; - int len; - if (remainingUntilEnd == 1) { - byte high = data.get(readIndex); - readIndex = 0; - byte low = data.get(readIndex); - readIndex = 1; - len = high << 8 | low; - remainingUntilEnd = len; - } else if (remainingUntilEnd == 2) { - len = data.getShort(readIndex); - readIndex = 0; - remainingUntilEnd = 0; - } else { - len = data.getShort(readIndex); - readIndex += 2; - remainingUntilEnd -= 2; + int readIndex = getReadIndex(); + try { + int remainingUntilEnd = capacity() - readIndex; + int len; + if (remainingUntilEnd == 1) { + byte high = get(readIndex); + readIndex = 0; + byte low = get(readIndex); + readIndex += 1; + len = high << 8 | low; + remainingUntilEnd = len; + } else if (remainingUntilEnd == 2) { + len = getShort(readIndex); + readIndex = 0; + remainingUntilEnd = 0; + } else { + len = getShort(readIndex); + readIndex += 2; + remainingUntilEnd -= 2; + } + byte[] result = new byte[len]; + if (len <= remainingUntilEnd) { + get(readIndex, result); + readIndex += len; + } else { + get(readIndex, result, 0, remainingUntilEnd); + readIndex = 0; + get(readIndex, result, remainingUntilEnd, len - remainingUntilEnd); + readIndex += len - remainingUntilEnd; + } + return result; + } finally { + setReadIndex(readIndex); } - byte[] result = new byte[len]; - if (len <= remainingUntilEnd) { - data.get(readIndex, result); - readIndex += len; - } else { - data.get(readIndex, result, 0, remainingUntilEnd); - readIndex = 0; - data.get(readIndex, result, remainingUntilEnd, len - remainingUntilEnd); - readIndex += len - remainingUntilEnd; - } - return result; + } + + private void get(int readIndex, byte[] result, int offset, int len) { + data.get(readIndex + START, result, offset, len); + } + + private void get(int readIndex, byte[] result) { + data.get(readIndex + START, result); + } + + private short getShort(int readIndex) { + return data.getShort(readIndex + START); + } + + private byte get(int readIndex) { + return data.get(readIndex + START); + } + + int getWriteIndex() { + return this.data.getInt(WRITE) - START; + } + + void setWriteIndex(int writeIndex) { + this.data.putInt(WRITE, writeIndex + START); + } + + int getReadIndex() { + return this.data.getInt(READ) - START; + } + + void setReadIndex(int readIndex) { + this.data.putInt(READ, readIndex + START); + } + + void putShort(int index, short value) { + this.data.putShort(index + START, value); + } + + void put(int index, byte value) { + this.data.put(index + START, value); + } + + void put(int index, byte[] value) { + this.data.put(index + START, value); + } + + private void put(int writeIndex, byte[] bytes, int offset, int len) { + data.put(writeIndex + START, bytes, offset, len - offset); } @Override public String toString() { - return "CircularBuffer {r=" + this.readIndex + + return "CircularByteBuffer {r=" + this.data.getInt(READ) + ", w=" + - this.writeIndex + + this.data.getInt(WRITE) + ", data=" + - IntStream.range(0, this.data.array().length) + IntStream.range(READ, this.data.array().length) .map(x -> this.data.array()[x]) .mapToObj(Integer::toString) .collect(Collectors.joining(",", "[", "]")) + diff --git a/lib/src/main/java/com/github/shautvast/exceptional/ExceptionLogger.java b/lib/src/main/java/com/github/shautvast/exceptional/ExceptionLogger.java index 303a283..052eafb 100644 --- a/lib/src/main/java/com/github/shautvast/exceptional/ExceptionLogger.java +++ b/lib/src/main/java/com/github/shautvast/exceptional/ExceptionLogger.java @@ -8,17 +8,21 @@ import java.lang.invoke.MethodHandle; @SuppressWarnings("unused") // this code is called from the instrumented code public class ExceptionLogger { private static final Arena arena = Arena.ofConfined(); + private static final MemorySegment ringbufferMemory = arena.allocate(4096); private static final Linker linker = Linker.nativeLinker(); // //TODO relative path, or configurable private static final SymbolLookup rustlib = SymbolLookup.libraryLookup("/Users/Shautvast/dev/exceptional/rustlib/target/debug/librustlib.dylib", arena); private final static MethodHandle logNative; private final static ObjectMapper objectMapper = new ObjectMapper(); + private final static MPSCBufferWriter bufferWriter; static { MemorySegment logFunction = rustlib.find("log_java_exception").orElseThrow(); logNative = linker.downcallHandle(logFunction, FunctionDescriptor.ofVoid( ValueLayout.ADDRESS )); + CircularByteBuffer buffer = new CircularByteBuffer(ringbufferMemory); + bufferWriter = new MPSCBufferWriter(buffer); } // how does this behave in a multithreaded context?? diff --git a/lib/src/main/java/com/github/shautvast/exceptional/RingBuffer.java b/lib/src/main/java/com/github/shautvast/exceptional/RingBuffer.java new file mode 100644 index 0000000..f89b99f --- /dev/null +++ b/lib/src/main/java/com/github/shautvast/exceptional/RingBuffer.java @@ -0,0 +1,179 @@ +package com.github.shautvast.exceptional; + +import java.lang.foreign.MemorySegment; +import java.nio.ByteBuffer; +import java.time.Duration; +import java.util.Optional; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; + +//circular MPSC buffer +//TODO REMOVE +public class RingBuffer implements AutoCloseable { + private final ByteBuffer memory; + private final AtomicInteger readPointer; + private final AtomicInteger writePointer; + private final AtomicBoolean writerThreadRunning = new AtomicBoolean(true); + private final AtomicBoolean readerThreadRunning = new AtomicBoolean(false); + private final ReentrantLock lock = new ReentrantLock(); + private static final LinkedBlockingDeque writeQueue = new LinkedBlockingDeque<>(); + private final ExecutorService writerThread; + private ExecutorService readerThread; + + void setReadPointer(int readPointer) { + this.readPointer.set(readPointer); + } + + public RingBuffer(MemorySegment memory) { + if (memory.byteSize() > 0xfffd) { + throw new IllegalArgumentException("Max memory size is 65533"); //TODO probably ffff + } + this.memory = memory.asByteBuffer(); + memory.asByteBuffer(); + readPointer = new AtomicInteger(0); + writePointer = new AtomicInteger(0); + + writerThread = Executors.newSingleThreadExecutor(); + writerThread.submit(() -> { + while (writerThreadRunning.get()) { + try { + byte[] data = writeQueue.poll(5, TimeUnit.SECONDS); + if (data != null) { + while (!writeBytes(data)) { + Thread.sleep(5000); + } + ; + } + } catch (InterruptedException _) { + // honor the interrupt + writerThreadRunning.set(false); + } + } + }); + + } + + + /** + * Writes a byte array to the ring buffer. + *

+ * If there is enough space in the buffer, the method writes the byte array to the buffer + * and returns true. If there is not enough space, the method does not write the byte array + * and returns false. + * + * @param data the byte array to write to the buffer + * @return true if the byte array was written successfully, false otherwise + */ + private boolean writeBytes(byte[] data) { + if (writePointer.get() > memory.capacity()) { + System.out.println("blocked"); + return false;//signal retry + } + System.out.println("write " + new String(data)); + int allocSize = data.length + 2; + int pos = writePointer.getAndAdd(allocSize); + if (writePointer.get() > (memory.capacity() - 2)) { + int max = memory.capacity() - (pos + 4); + if (data.length - max < readPointer.get()) { + System.out.println("wrap"); + memory.putShort(pos, (short) data.length); + memory.position(pos + 2); + memory.put(data, 0, max); + memory.position(0); + memory.put(data, max, data.length - max); + writePointer.set(data.length - max); + memory.putShort((short) 0); + return true; + } else { + return false; + } + } else { + memory.putShort(pos, (short) data.length); + memory.position(pos + 2); + memory.put(data); + memory.putShort((short) 0); + return true; + } + } + + /** + * Reads a byte array from the ring buffer with a specified timeout. + *

+ * Blocks until there is data available to read or the timeout is reached. + * If the timeout is reached and there is still no data, the resul is empty. + * + * @param timeout the maximum time to wait for data to be available in the buffer + * @return the byte array read from the buffer + * @throws InterruptedException if the thread is interrupted while waiting for data + */ + private Optional read(Duration timeout) throws InterruptedException { + if (memory.getShort(readPointer.get()) == 0 || readPointer.get() >= memory.capacity()) { + return Optional.empty(); + } + return Optional.ofNullable(getBytes()); + } + + private byte[] getBytes() { + int currentReadPointerValue = readPointer.get(); + int lenToread = memory.getShort(currentReadPointerValue); + System.out.println(lenToread + " bytes"); + if (lenToread <= 0) { + return null; + } + currentReadPointerValue = readPointer.addAndGet(2); + byte[] data = new byte[lenToread]; + int bytesTilEnd = memory.capacity() - currentReadPointerValue; + if (lenToread > bytesTilEnd) { + memory.get(currentReadPointerValue, data, 0, bytesTilEnd); + memory.get(0, data, bytesTilEnd, lenToread - bytesTilEnd); + readPointer.set(lenToread - bytesTilEnd); + } else { + memory.get(currentReadPointerValue, data); + System.out.println("set "+readPointer.addAndGet(lenToread)); + } + return data; + } + + + public void write(byte[] bytes) { + while (!writeQueue.offer(bytes)) ; + } + + public void startReader(Consumer consumer) { + readerThreadRunning.set(true); + readerThread = Executors.newSingleThreadExecutor(); + + readerThread.submit(() -> { + while (readerThreadRunning.get()) { + try { + System.out.println("read"); + read(Duration.ofSeconds(5)).ifPresent(consumer); + Thread.sleep(5000); + } catch (InterruptedException _) { + readerThreadRunning.set(false); + } + } + }); + + } + + public void close() { + System.out.println("close"); + writerThreadRunning.set(false); + readerThreadRunning.set(false); + writerThread.close(); + readerThread.close(); + } + + public void drain() { + while (!writeQueue.isEmpty()) ; + close(); + } +} + diff --git a/lib/src/main/java/jacob/CborConstants.java b/lib/src/main/java/jacob/CborConstants.java new file mode 100644 index 0000000..df9fabd --- /dev/null +++ b/lib/src/main/java/jacob/CborConstants.java @@ -0,0 +1,89 @@ +/* + * JACOB - CBOR implementation in Java. + * + * (C) Copyright - 2013 - J.W. Janssen + * + * Licensed under Apache License v2.0. + */ +package jacob; + +/** + * Constant values used by the CBOR format. + */ +public interface CborConstants { + /** Major type 0: unsigned integers. */ + int TYPE_UNSIGNED_INTEGER = 0x00; + /** Major type 1: negative integers. */ + int TYPE_NEGATIVE_INTEGER = 0x01; + /** Major type 2: byte string. */ + int TYPE_BYTE_STRING = 0x02; + /** Major type 3: text/UTF8 string. */ + int TYPE_TEXT_STRING = 0x03; + /** Major type 4: array of items. */ + int TYPE_ARRAY = 0x04; + /** Major type 5: map of pairs. */ + int TYPE_MAP = 0x05; + /** Major type 6: semantic tags. */ + int TYPE_TAG = 0x06; + /** Major type 7: floating point, simple data types. */ + int TYPE_FLOAT_SIMPLE = 0x07; + + /** Denotes a one-byte value (uint8). */ + int ONE_BYTE = 0x18; + /** Denotes a two-byte value (uint16). */ + int TWO_BYTES = 0x19; + /** Denotes a four-byte value (uint32). */ + int FOUR_BYTES = 0x1a; + /** Denotes a eight-byte value (uint64). */ + int EIGHT_BYTES = 0x1b; + + /** The CBOR-encoded boolean false value (encoded as "simple value": {@link #MT_SIMPLE}). */ + int FALSE = 0x14; + /** The CBOR-encoded boolean true value (encoded as "simple value": {@link #MT_SIMPLE}). */ + int TRUE = 0x15; + /** The CBOR-encoded null value (encoded as "simple value": {@link #MT_SIMPLE}). */ + int NULL = 0x16; + /** The CBOR-encoded "undefined" value (encoded as "simple value": {@link #MT_SIMPLE}). */ + int UNDEFINED = 0x17; + /** Denotes a half-precision float (two-byte IEEE 754, see {@link #MT_FLOAT}). */ + int HALF_PRECISION_FLOAT = 0x19; + /** Denotes a single-precision float (four-byte IEEE 754, see {@link #MT_FLOAT}). */ + int SINGLE_PRECISION_FLOAT = 0x1a; + /** Denotes a double-precision float (eight-byte IEEE 754, see {@link #MT_FLOAT}). */ + int DOUBLE_PRECISION_FLOAT = 0x1b; + /** The CBOR-encoded "break" stop code for unlimited arrays/maps. */ + int BREAK = 0x1f; + + /** Semantic tag value describing date/time values in the standard format (UTF8 string, RFC3339). */ + int TAG_STANDARD_DATE_TIME = 0; + /** Semantic tag value describing date/time values as Epoch timestamp (numeric, RFC3339). */ + int TAG_EPOCH_DATE_TIME = 1; + /** Semantic tag value describing a positive big integer value (byte string). */ + int TAG_POSITIVE_BIGINT = 2; + /** Semantic tag value describing a negative big integer value (byte string). */ + int TAG_NEGATIVE_BIGINT = 3; + /** Semantic tag value describing a decimal fraction value (two-element array, base 10). */ + int TAG_DECIMAL_FRACTION = 4; + /** Semantic tag value describing a big decimal value (two-element array, base 2). */ + int TAG_BIGDECIMAL = 5; + /** Semantic tag value describing an expected conversion to base64url encoding. */ + int TAG_EXPECTED_BASE64_URL_ENCODED = 21; + /** Semantic tag value describing an expected conversion to base64 encoding. */ + int TAG_EXPECTED_BASE64_ENCODED = 22; + /** Semantic tag value describing an expected conversion to base16 encoding. */ + int TAG_EXPECTED_BASE16_ENCODED = 23; + /** Semantic tag value describing an encoded CBOR data item (byte string). */ + int TAG_CBOR_ENCODED = 24; + /** Semantic tag value describing an URL (UTF8 string). */ + int TAG_URI = 32; + /** Semantic tag value describing a base64url encoded string (UTF8 string). */ + int TAG_BASE64_URL_ENCODED = 33; + /** Semantic tag value describing a base64 encoded string (UTF8 string). */ + int TAG_BASE64_ENCODED = 34; + /** Semantic tag value describing a regular expression string (UTF8 string, PCRE). */ + int TAG_REGEXP = 35; + /** Semantic tag value describing a MIME message (UTF8 string, RFC2045). */ + int TAG_MIME_MESSAGE = 36; + /** Semantic tag value describing CBOR content. */ + int TAG_CBOR_MARKER = 55799; +} diff --git a/lib/src/main/java/jacob/CborDecoder.java b/lib/src/main/java/jacob/CborDecoder.java new file mode 100644 index 0000000..e0969a4 --- /dev/null +++ b/lib/src/main/java/jacob/CborDecoder.java @@ -0,0 +1,498 @@ +/* + * JACOB - CBOR implementation in Java. + * + * (C) Copyright - 2013 - J.W. Janssen + */ +package jacob; + +import static jacob.CborConstants.*; +import static jacob.CborType.*; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; + +/** + * Provides a decoder capable of handling CBOR encoded data from a {@link InputStream}. + */ +public class CborDecoder { + protected final PushbackInputStream m_is; + + /** + * Creates a new {@link CborDecoder} instance. + * + * @param is the actual input stream to read the CBOR-encoded data from, cannot be null. + */ + public CborDecoder(InputStream is) { + if (is == null) { + throw new IllegalArgumentException("InputStream cannot be null!"); + } + m_is = (is instanceof PushbackInputStream) ? (PushbackInputStream) is : new PushbackInputStream(is); + } + + private static void fail(String msg, Object... args) throws IOException { + throw new IOException(String.format(msg, args)); + } + + private static String lengthToString(int len) { + return (len < 0) ? "no payload" : (len == ONE_BYTE) ? "one byte" : (len == TWO_BYTES) ? "two bytes" + : (len == FOUR_BYTES) ? "four bytes" : (len == EIGHT_BYTES) ? "eight bytes" : "(unknown)"; + } + + /** + * Peeks in the input stream for the upcoming type. + * + * @return the upcoming type in the stream, or null in case of an end-of-stream. + * @throws IOException in case of I/O problems reading the CBOR-type from the underlying input stream. + */ + public CborType peekType() throws IOException { + int p = m_is.read(); + if (p < 0) { + // EOF, nothing to peek at... + return null; + } + m_is.unread(p); + return valueOf(p); + } + + /** + * Prolog to reading an array value in CBOR format. + * + * @return the number of elements in the array to read, or -1 in case of infinite-length arrays. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readArrayLength() throws IOException { + return readMajorTypeWithSize(TYPE_ARRAY); + } + + /** + * Reads a boolean value in CBOR format. + * + * @return the read boolean. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public boolean readBoolean() throws IOException { + int b = readMajorType(TYPE_FLOAT_SIMPLE); + if (b != FALSE && b != TRUE) { + fail("Unexpected boolean value: %d!", b); + } + return b == TRUE; + } + + /** + * Reads a "break"/stop value in CBOR format. + * + * @return always null. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public Object readBreak() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, BREAK); + + return null; + } + + /** + * Reads a byte string value in CBOR format. + * + * @return the read byte string, never null. In case the encoded string has a length of 0, an empty string is returned. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public byte[] readByteString() throws IOException { + long len = readMajorTypeWithSize(TYPE_BYTE_STRING); + if (len < 0) { + fail("Infinite-length byte strings not supported!"); + } + if (len > Integer.MAX_VALUE) { + fail("String length too long!"); + } + return readFully(new byte[(int) len]); + } + + /** + * Prolog to reading a byte string value in CBOR format. + * + * @return the number of bytes in the string to read, or -1 in case of infinite-length strings. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readByteStringLength() throws IOException { + return readMajorTypeWithSize(TYPE_BYTE_STRING); + } + + /** + * Reads a double-precision float value in CBOR format. + * + * @return the read double value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public double readDouble() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, DOUBLE_PRECISION_FLOAT); + + return Double.longBitsToDouble(readUInt64()); + } + + /** + * Reads a single-precision float value in CBOR format. + * + * @return the read float value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public float readFloat() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, SINGLE_PRECISION_FLOAT); + + return Float.intBitsToFloat((int) readUInt32()); + } + + /** + * Reads a half-precision float value in CBOR format. + * + * @return the read half-precision float value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public double readHalfPrecisionFloat() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, HALF_PRECISION_FLOAT); + + int half = readUInt16(); + int exp = (half >> 10) & 0x1f; + int mant = half & 0x3ff; + + double val; + if (exp == 0) { + val = mant * Math.pow(2, -24); + } else if (exp != 31) { + val = (mant + 1024) * Math.pow(2, exp - 25); + } else if (mant != 0) { + val = Double.NaN; + } else { + val = Double.POSITIVE_INFINITY; + } + + return ((half & 0x8000) == 0) ? val : -val; + } + + /** + * Reads a signed or unsigned integer value in CBOR format. + * + * @return the read integer value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readInt() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return ui ^ readUInt(ib & 0x1f, false /* breakAllowed */); + } + + /** + * Reads a signed or unsigned 16-bit integer value in CBOR format. + * + * @read the small integer value, values from [-65536..65535] are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream. + */ + public int readInt16() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return (int) (ui ^ readUIntExact(TWO_BYTES, ib & 0x1f)); + } + + /** + * Reads a signed or unsigned 32-bit integer value in CBOR format. + * + * @read the small integer value, values in the range [-4294967296..4294967295] are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream. + */ + public long readInt32() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return ui ^ readUIntExact(FOUR_BYTES, ib & 0x1f); + } + + /** + * Reads a signed or unsigned 64-bit integer value in CBOR format. + * + * @read the small integer value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream. + */ + public long readInt64() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return ui ^ readUIntExact(EIGHT_BYTES, ib & 0x1f); + } + + /** + * Reads a signed or unsigned 8-bit integer value in CBOR format. + * + * @read the small integer value, values in the range [-256..255] are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream. + */ + public int readInt8() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return (int) (ui ^ readUIntExact(ONE_BYTE, ib & 0x1f)); + } + + /** + * Prolog to reading a map of key-value pairs in CBOR format. + * + * @return the number of entries in the map, >= 0. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readMapLength() throws IOException { + return readMajorTypeWithSize(TYPE_MAP); + } + + /** + * Reads a null-value in CBOR format. + * + * @return always null. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public Object readNull() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, NULL); + return null; + } + + /** + * Reads a single byte value in CBOR format. + * + * @return the read byte value. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public byte readSimpleValue() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, ONE_BYTE); + return (byte) readUInt8(); + } + + /** + * Reads a signed or unsigned small (<= 23) integer value in CBOR format. + * + * @read the small integer value, values in the range [-24..23] are supported. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream. + */ + public int readSmallInt() throws IOException { + int ib = m_is.read(); + + // in case of negative integers, extends the sign to all bits; otherwise zero... + long ui = expectIntegerType(ib); + // in case of negative integers does a ones complement + return (int) (ui ^ readUIntExact(-1, ib & 0x1f)); + } + + /** + * Reads a semantic tag value in CBOR format. + * + * @return the read tag value. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readTag() throws IOException { + return readUInt(readMajorType(TYPE_TAG), false /* breakAllowed */); + } + + /** + * Reads an UTF-8 encoded string value in CBOR format. + * + * @return the read UTF-8 encoded string, never null. In case the encoded string has a length of 0, an empty string is returned. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public String readTextString() throws IOException { + long len = readMajorTypeWithSize(TYPE_TEXT_STRING); + if (len < 0) { + fail("Infinite-length text strings not supported!"); + } + if (len > Integer.MAX_VALUE) { + fail("String length too long!"); + } + return new String(readFully(new byte[(int) len]), "UTF-8"); + } + + /** + * Prolog to reading an UTF-8 encoded string value in CBOR format. + * + * @return the length of the string to read, or -1 in case of infinite-length strings. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public long readTextStringLength() throws IOException { + return readMajorTypeWithSize(TYPE_TEXT_STRING); + } + + /** + * Reads an undefined value in CBOR format. + * + * @return always null. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + public Object readUndefined() throws IOException { + readMajorTypeExact(TYPE_FLOAT_SIMPLE, UNDEFINED); + return null; + } + + /** + * Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation. + * + * @param majorType the expected major type, cannot be null (unchecked). + * @return either -1 if the major type was an signed integer, or 0 otherwise. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + protected long expectIntegerType(int ib) throws IOException { + int majorType = ((ib & 0xFF) >>> 5); + if ((majorType != TYPE_UNSIGNED_INTEGER) && (majorType != TYPE_NEGATIVE_INTEGER)) { + fail("Unexpected type: %s, expected type %s or %s!", getName(majorType), getName(TYPE_UNSIGNED_INTEGER), + getName(TYPE_NEGATIVE_INTEGER)); + } + return -majorType; + } + + /** + * Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation. + * + * @param majorType the expected major type, cannot be null (unchecked). + * @return the read subtype, or payload, of the read major type. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + protected int readMajorType(int majorType) throws IOException { + int ib = m_is.read(); + if (majorType != ((ib >>> 5) & 0x07)) { + fail("Unexpected type: %s, expected: %s!", getName(ib), getName(majorType)); + } + return ib & 0x1F; + } + + /** + * Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations. + * + * @param majorType the expected major type, cannot be null (unchecked); + * @param subtype the expected subtype. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + protected void readMajorTypeExact(int majorType, int subtype) throws IOException { + int st = readMajorType(majorType); + if ((st ^ subtype) != 0) { + fail("Unexpected subtype: %d, expected: %d!", st, subtype); + } + } + + /** + * Reads the next major type from the underlying input stream, verifies whether it matches the given expectation, and decodes the payload into a size. + * + * @param majorType the expected major type, cannot be null (unchecked). + * @return the number of succeeding bytes, >= 0, or -1 if an infinite-length type is read. + * @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream. + */ + protected long readMajorTypeWithSize(int majorType) throws IOException { + return readUInt(readMajorType(majorType), true /* breakAllowed */); + } + + /** + * Reads an unsigned integer with a given length-indicator. + * + * @param length the length indicator to use; + * @return the read unsigned integer, as long value. + * @throws IOException in case of I/O problems reading the unsigned integer from the underlying input stream. + */ + protected long readUInt(int length, boolean breakAllowed) throws IOException { + long result = -1; + if (length < ONE_BYTE) { + result = length; + } else if (length == ONE_BYTE) { + result = readUInt8(); + } else if (length == TWO_BYTES) { + result = readUInt16(); + } else if (length == FOUR_BYTES) { + result = readUInt32(); + } else if (length == EIGHT_BYTES) { + result = readUInt64(); + } else if (breakAllowed && length == BREAK) { + return -1; + } + if (result < 0) { + fail("Not well-formed CBOR integer found, invalid length: %d!", result); + } + return result; + } + + /** + * Reads an unsigned 16-bit integer value + * + * @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected int readUInt16() throws IOException { + byte[] buf = readFully(new byte[2]); + return (buf[0] & 0xFF) << 8 | (buf[1] & 0xFF); + } + + /** + * Reads an unsigned 32-bit integer value + * + * @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected long readUInt32() throws IOException { + byte[] buf = readFully(new byte[4]); + return ((buf[0] & 0xFF) << 24 | (buf[1] & 0xFF) << 16 | (buf[2] & 0xFF) << 8 | (buf[3] & 0xFF)) & 0xffffffffL; + } + + /** + * Reads an unsigned 64-bit integer value + * + * @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected long readUInt64() throws IOException { + byte[] buf = readFully(new byte[8]); + return (buf[0] & 0xFFL) << 56 | (buf[1] & 0xFFL) << 48 | (buf[2] & 0xFFL) << 40 | (buf[3] & 0xFFL) << 32 | // + (buf[4] & 0xFFL) << 24 | (buf[5] & 0xFFL) << 16 | (buf[6] & 0xFFL) << 8 | (buf[7] & 0xFFL); + } + + /** + * Reads an unsigned 8-bit integer value + * + * @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected int readUInt8() throws IOException { + return m_is.read() & 0xff; + } + + /** + * Reads an unsigned integer with a given length-indicator. + * + * @param length the length indicator to use; + * @return the read unsigned integer, as long value. + * @throws IOException in case of I/O problems reading the unsigned integer from the underlying input stream. + */ + protected long readUIntExact(int expectedLength, int length) throws IOException { + if (((expectedLength == -1) && (length >= ONE_BYTE)) || ((expectedLength >= 0) && (length != expectedLength))) { + fail("Unexpected payload/length! Expected %s, but got %s.", lengthToString(expectedLength), + lengthToString(length)); + } + return readUInt(length, false /* breakAllowed */); + } + + private byte[] readFully(byte[] buf) throws IOException { + int len = buf.length; + int n = 0, off = 0; + while (n < len) { + int count = m_is.read(buf, off + n, len - n); + if (count < 0) { + throw new EOFException(); + } + n += count; + } + return buf; + } +} diff --git a/lib/src/main/java/jacob/CborEncoder.java b/lib/src/main/java/jacob/CborEncoder.java new file mode 100644 index 0000000..b09aafe --- /dev/null +++ b/lib/src/main/java/jacob/CborEncoder.java @@ -0,0 +1,488 @@ +/* + * JACOB - CBOR implementation in Java. + * + * (C) Copyright - 2013 - J.W. Janssen + * + * Licensed under Apache License v2.0. + */ +package jacob; + +import static jacob.CborConstants.*; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Provides an encoder capable of encoding data into CBOR format to a given {@link OutputStream}. + */ +public class CborEncoder { + private static final int NEG_INT_MASK = TYPE_NEGATIVE_INTEGER << 5; + + private final OutputStream m_os; + + /** + * Creates a new {@link CborEncoder} instance. + * + * @param os the actual output stream to write the CBOR-encoded data to, cannot be null. + */ + public CborEncoder(OutputStream os) { + if (os == null) { + throw new IllegalArgumentException("OutputStream cannot be null!"); + } + m_os = os; + } + + /** + * Interprets a given float-value as a half-precision float value and + * converts it to its raw integer form, as defined in IEEE 754. + *

+ * Taken from: this Stack Overflow answer. + *

+ * + * @param fval the value to convert. + * @return the raw integer representation of the given float value. + */ + static int halfPrecisionToRawIntBits(float fval) { + int fbits = Float.floatToIntBits(fval); + int sign = (fbits >>> 16) & 0x8000; + int val = (fbits & 0x7fffffff) + 0x1000; + + // might be or become NaN/Inf + if (val >= 0x47800000) { + if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become NaN/Inf + if (val < 0x7f800000) { + // was value but too large, make it +/-Inf + return sign | 0x7c00; + } + return sign | 0x7c00 | (fbits & 0x007fffff) >>> 13; // keep NaN (and Inf) bits + } + return sign | 0x7bff; // unrounded not quite Inf + } + if (val >= 0x38800000) { + // remains normalized value + return sign | val - 0x38000000 >>> 13; // exp - 127 + 15 + } + if (val < 0x33000000) { + // too small for subnormal + return sign; // becomes +/-0 + } + + val = (fbits & 0x7fffffff) >>> 23; + // add subnormal bit, round depending on cut off and div by 2^(1-(exp-127+15)) and >> 13 | exp=0 + return sign | ((fbits & 0x7fffff | 0x800000) + (0x800000 >>> val - 102) >>> 126 - val); + } + + /** + * Writes the start of an indefinite-length array. + *

+ * After calling this method, one is expected to write the given number of array elements, which can be of any type. No length checks are performed.
+ * After all array elements are written, one should write a single break value to end the array, see {@link #writeBreak()}. + *

+ * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeArrayStart() throws IOException { + writeSimpleType(TYPE_ARRAY, BREAK); + } + + /** + * Writes the start of a definite-length array. + *

+ * After calling this method, one is expected to write the given number of array elements, which can be of any type. No length checks are performed. + *

+ * + * @param length the number of array elements to write, should >= 0. + * @throws IllegalArgumentException in case the given length was negative; + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeArrayStart(int length) throws IOException { + if (length < 0) { + throw new IllegalArgumentException("Invalid array-length!"); + } + writeType(TYPE_ARRAY, length); + } + + /** + * Writes a boolean value in canonical CBOR format. + * + * @param value the boolean to write. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeBoolean(boolean value) throws IOException { + writeSimpleType(TYPE_FLOAT_SIMPLE, value ? TRUE : FALSE); + } + + /** + * Writes a "break" stop-value in canonical CBOR format. + * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeBreak() throws IOException { + writeSimpleType(TYPE_FLOAT_SIMPLE, BREAK); + } + + /** + * Writes a byte string in canonical CBOR-format. + * + * @param value the byte string to write, can be null in which case a byte-string of length 0 is written. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeByteString(byte[] bytes) throws IOException { + writeString(TYPE_BYTE_STRING, bytes); + } + + /** + * Writes the start of an indefinite-length byte string. + *

+ * After calling this method, one is expected to write the given number of string parts. No length checks are performed.
+ * After all string parts are written, one should write a single break value to end the string, see {@link #writeBreak()}. + *

+ * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeByteStringStart() throws IOException { + writeSimpleType(TYPE_BYTE_STRING, BREAK); + } + + /** + * Writes a double-precision float value in canonical CBOR format. + * + * @param value the value to write, values from {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeDouble(double value) throws IOException { + writeUInt64(TYPE_FLOAT_SIMPLE << 5, Double.doubleToRawLongBits(value)); + } + + /** + * Writes a single-precision float value in canonical CBOR format. + * + * @param value the value to write, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeFloat(float value) throws IOException { + writeUInt32(TYPE_FLOAT_SIMPLE << 5, Float.floatToRawIntBits(value)); + } + + /** + * Writes a half-precision float value in canonical CBOR format. + * + * @param value the value to write, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeHalfPrecisionFloat(float value) throws IOException { + writeUInt16(TYPE_FLOAT_SIMPLE << 5, halfPrecisionToRawIntBits(value)); + } + + /** + * Writes a signed or unsigned integer value in canonical CBOR format, that is, tries to encode it in a little bytes as possible.. + * + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeInt(long value) throws IOException { + // extends the sign over all bits... + long sign = value >> 63; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + value = (sign ^ value); + + writeUInt(mt, value); + } + + /** + * Writes a signed or unsigned 16-bit integer value in CBOR format. + * + * @param value the value to write, values from [-65536..65535] are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeInt16(int value) throws IOException { + // extends the sign over all bits... + int sign = value >> 31; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + writeUInt16(mt, (sign ^ value) & 0xffff); + } + + /** + * Writes a signed or unsigned 32-bit integer value in CBOR format. + * + * @param value the value to write, values in the range [-4294967296..4294967295] are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeInt32(long value) throws IOException { + // extends the sign over all bits... + long sign = value >> 63; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + writeUInt32(mt, (int) ((sign ^ value) & 0xffffffffL)); + } + + /** + * Writes a signed or unsigned 64-bit integer value in CBOR format. + * + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeInt64(long value) throws IOException { + // extends the sign over all bits... + long sign = value >> 63; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + writeUInt64(mt, sign ^ value); + } + + /** + * Writes a signed or unsigned 8-bit integer value in CBOR format. + * + * @param value the value to write, values in the range [-256..255] are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeInt8(int value) throws IOException { + // extends the sign over all bits... + int sign = value >> 31; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + writeUInt8(mt, (sign ^ value) & 0xff); + } + + /** + * Writes the start of an indefinite-length map. + *

+ * After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed.
+ * After all map entries are written, one should write a single break value to end the map, see {@link #writeBreak()}. + *

+ * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeMapStart() throws IOException { + writeSimpleType(TYPE_MAP, BREAK); + } + + /** + * Writes the start of a finite-length map. + *

+ * After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed. + *

+ * + * @param length the number of map entries to write, should >= 0. + * @throws IllegalArgumentException in case the given length was negative; + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeMapStart(int length) throws IOException { + if (length < 0) { + throw new IllegalArgumentException("Invalid length of map!"); + } + writeType(TYPE_MAP, length); + } + + /** + * Writes a null value in canonical CBOR format. + * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeNull() throws IOException { + writeSimpleType(TYPE_FLOAT_SIMPLE, NULL); + } + + /** + * Writes a simple value, i.e., an "atom" or "constant" value in canonical CBOR format. + * + * @param value the (unsigned byte) value to write, values from 32 to 255 are supported (though not enforced). + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeSimpleValue(byte simpleValue) throws IOException { + // convert to unsigned value... + int value = (simpleValue & 0xff); + writeType(TYPE_FLOAT_SIMPLE, value); + } + + /** + * Writes a signed or unsigned small (<= 23) integer value in CBOR format. + * + * @param value the value to write, values in the range [-24..23] are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeSmallInt(int value) throws IOException { + // extends the sign over all bits... + int sign = value >> 31; + // in case value is negative, this bit should be set... + int mt = (int) (sign & NEG_INT_MASK); + // complement negative value... + value = Math.min(0x17, (sign ^ value)); + + m_os.write((int) (mt | value)); + } + + /** + * Writes a semantic tag in canonical CBOR format. + * + * @param tag the tag to write, should >= 0. + * @throws IllegalArgumentException in case the given tag was negative; + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeTag(long tag) throws IOException { + if (tag < 0) { + throw new IllegalArgumentException("Invalid tag specification, cannot be negative!"); + } + writeType(TYPE_TAG, tag); + } + + /** + * Writes an UTF-8 string in canonical CBOR-format. + *

+ * Note that this method is platform specific, as the given string value will be encoded in a byte array + * using the platform encoding! This means that the encoding must be standardized and known. + *

+ * + * @param value the UTF-8 string to write, can be null in which case an UTF-8 string of length 0 is written. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeTextString(String value) throws IOException { + writeString(TYPE_TEXT_STRING, value == null ? null : value.getBytes("UTF-8")); + } + + /** + * Writes the start of an indefinite-length UTF-8 string. + *

+ * After calling this method, one is expected to write the given number of string parts. No length checks are performed.
+ * After all string parts are written, one should write a single break value to end the string, see {@link #writeBreak()}. + *

+ * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeTextStringStart() throws IOException { + writeSimpleType(TYPE_TEXT_STRING, BREAK); + } + + /** + * Writes an "undefined" value in canonical CBOR format. + * + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + public void writeUndefined() throws IOException { + writeSimpleType(TYPE_FLOAT_SIMPLE, UNDEFINED); + } + + /** + * Encodes and writes the major type and value as a simple type. + * + * @param majorType the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from [0..31] are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeSimpleType(int majorType, int value) throws IOException { + m_os.write((majorType << 5) | (value & 0x1f)); + } + + /** + * Writes a byte string in canonical CBOR-format. + * + * @param majorType the major type of the string, should be either 0x40 or 0x60; + * @param value the byte string to write, can be null in which case a byte-string of length 0 is written. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeString(int majorType, byte[] bytes) throws IOException { + int len = (bytes == null) ? 0 : bytes.length; + writeType(majorType, len); + for (int i = 0; i < len; i++) { + m_os.write(bytes[i]); + } + } + + /** + * Encodes and writes the major type indicator with a given payload (length). + * + * @param majorType the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeType(int majorType, long value) throws IOException { + writeUInt((majorType << 5), value); + } + + /** + * Encodes and writes an unsigned integer value, that is, tries to encode it in a little bytes as possible. + * + * @param mt the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeUInt(int mt, long value) throws IOException { + if (value < 0x18L) { + m_os.write((int) (mt | value)); + } else if (value < 0x100L) { + writeUInt8(mt, (int) value); + } else if (value < 0x10000L) { + writeUInt16(mt, (int) value); + } else if (value < 0x100000000L) { + writeUInt32(mt, (int) value); + } else { + writeUInt64(mt, value); + } + } + + /** + * Encodes and writes an unsigned 16-bit integer value + * + * @param mt the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeUInt16(int mt, int value) throws IOException { + m_os.write(mt | TWO_BYTES); + m_os.write(value >> 8); + m_os.write(value & 0xFF); + } + + /** + * Encodes and writes an unsigned 32-bit integer value + * + * @param mt the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeUInt32(int mt, int value) throws IOException { + m_os.write(mt | FOUR_BYTES); + m_os.write(value >> 24); + m_os.write(value >> 16); + m_os.write(value >> 8); + m_os.write(value & 0xFF); + } + + /** + * Encodes and writes an unsigned 64-bit integer value + * + * @param mt the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeUInt64(int mt, long value) throws IOException { + m_os.write(mt | EIGHT_BYTES); + m_os.write((int) (value >> 56)); + m_os.write((int) (value >> 48)); + m_os.write((int) (value >> 40)); + m_os.write((int) (value >> 32)); + m_os.write((int) (value >> 24)); + m_os.write((int) (value >> 16)); + m_os.write((int) (value >> 8)); + m_os.write((int) (value & 0xFF)); + } + + /** + * Encodes and writes an unsigned 8-bit integer value + * + * @param mt the major type of the value to write, denotes what semantics the written value has; + * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported. + * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream. + */ + protected void writeUInt8(int mt, int value) throws IOException { + m_os.write(mt | ONE_BYTE); + m_os.write(value & 0xFF); + } +} diff --git a/lib/src/main/java/jacob/CborType.java b/lib/src/main/java/jacob/CborType.java new file mode 100644 index 0000000..70338c1 --- /dev/null +++ b/lib/src/main/java/jacob/CborType.java @@ -0,0 +1,142 @@ +/* + * JACOB - CBOR implementation in Java. + * + * (C) Copyright - 2013 - J.W. Janssen + * + * Licensed under Apache License v2.0. + */ +package jacob; + +import static jacob.CborConstants.*; + +/** + * Represents the various major types in CBOR, along with their . + *

+ * The major type is encoded in the upper three bits of each initial byte. The lower 5 bytes represent any additional information. + *

+ */ +public class CborType { + private final int m_major; + private final int m_additional; + + private CborType(int major, int additional) { + m_major = major; + m_additional = additional; + } + + /** + * Returns a descriptive string for the given major type. + * + * @param mt the major type to return as string, values from [0..7] are supported. + * @return the name of the given major type, as String, never null. + * @throws IllegalArgumentException in case the given major type is not supported. + */ + public static String getName(int mt) { + switch (mt) { + case TYPE_ARRAY: + return "array"; + case TYPE_BYTE_STRING: + return "byte string"; + case TYPE_FLOAT_SIMPLE: + return "float/simple value"; + case TYPE_MAP: + return "map"; + case TYPE_NEGATIVE_INTEGER: + return "negative integer"; + case TYPE_TAG: + return "tag"; + case TYPE_TEXT_STRING: + return "text string"; + case TYPE_UNSIGNED_INTEGER: + return "unsigned integer"; + default: + throw new IllegalArgumentException("Invalid major type: " + mt); + } + } + + /** + * Decodes a given byte value to a {@link CborType} value. + * + * @param i the input byte (8-bit) to decode into a {@link CborType} instance. + * @return a {@link CborType} instance, never null. + */ + public static CborType valueOf(int i) { + return new CborType((i & 0xff) >>> 5, i & 0x1f); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + CborType other = (CborType) obj; + return (m_major == other.m_major) && (m_additional == other.m_additional); + } + + /** + * @return the additional information of this type, as integer value from [0..31]. + */ + public int getAdditionalInfo() { + return m_additional; + } + + /** + * @return the major type, as integer value from [0..7]. + */ + public int getMajorType() { + return m_major; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + m_additional; + result = prime * result + m_major; + return result; + } + + /** + * @return true if this type allows for an infinite-length payload, + * false if only definite-length payloads are allowed. + */ + public boolean isBreakAllowed() { + return m_major == TYPE_ARRAY || m_major == TYPE_BYTE_STRING || m_major == TYPE_MAP + || m_major == TYPE_TEXT_STRING; + } + + /** + * Determines whether the major type of a given {@link CborType} equals the major type of this {@link CborType}. + * + * @param other the {@link CborType} to compare against, cannot be null. + * @return true if the given {@link CborType} is of the same major type as this {@link CborType}, false otherwise. + * @throws IllegalArgumentException in case the given argument was null. + */ + public boolean isEqualType(CborType other) { + if (other == null) { + throw new IllegalArgumentException("Parameter cannot be null!"); + } + return m_major == other.m_major; + } + + /** + * Determines whether the major type of a given byte value (representing an encoded {@link CborType}) equals the major type of this {@link CborType}. + * + * @param encoded the encoded CBOR type to compare. + * @return true if the given byte value represents the same major type as this {@link CborType}, false otherwise. + */ + public boolean isEqualType(int encoded) { + return m_major == ((encoded & 0xff) >>> 5); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getName(m_major)).append('(').append(m_additional).append(')'); + return sb.toString(); + } +} diff --git a/lib/src/test/java/com/github/shautvast/exceptional/CircularByteBufferTest.java b/lib/src/test/java/com/github/shautvast/exceptional/CircularByteBufferTest.java index 1ef927c..b5ae3cb 100644 --- a/lib/src/test/java/com/github/shautvast/exceptional/CircularByteBufferTest.java +++ b/lib/src/test/java/com/github/shautvast/exceptional/CircularByteBufferTest.java @@ -14,48 +14,51 @@ class CircularByteBufferTest { boolean written = buffer.put(bytes); assertTrue(written); assertArrayEquals(bytes, buffer.get()); - assertArrayEquals(new byte[]{0, 5, 104, 101, 108, 108, 111, 0, 0}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 15, 0, 0, 0, 15, 0, 5, 104, 101, 108, 108, 111, 0, 0}, buffer.data.array()); } + @Test void testPutFitsBeforeGet() { CircularByteBuffer buffer = new CircularByteBuffer(14); byte[] bytes = "hello".getBytes(UTF_8); - buffer.writeIndex = 7; - buffer.readIndex = 7; + buffer.setWriteIndex(7); + buffer.setReadIndex(7); buffer.put(bytes); - assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); - buffer.writeIndex = 0; + assertArrayEquals(new byte[]{0, 0, 0, 15, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); +// buffer.setWriteIndex(0); // end of setup, situation where writeIndex < readIndex boolean written = buffer.put(bytes); assertTrue(written); - assertArrayEquals(new byte[]{0, 5, 104, 101, 108, 108, 111, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 15, 0, 0, 0, 15, 0, 5, 104, 101, 108, 108, 111, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); + assertEquals(7, buffer.getReadIndex()); + assertEquals(7, buffer.getWriteIndex()); } @Test void testPutFitsNotBeforeGet() { CircularByteBuffer buffer = new CircularByteBuffer(13); byte[] bytes = "hello".getBytes(UTF_8); - buffer.writeIndex = 6; - buffer.readIndex = 6; + buffer.setWriteIndex(6); + buffer.setReadIndex(6); buffer.put(bytes); - assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); - buffer.writeIndex = 0; + assertArrayEquals(new byte[]{0, 0, 0, 14, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); + // end of setup, situation where writeIndex < readIndex boolean written = buffer.put(bytes); assertFalse(written); - assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 14, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111}, buffer.data.array()); } @Test void testWrapAroundPutLenAndOneCharBeforeWrap() { CircularByteBuffer buffer = new CircularByteBuffer(9); byte[] bytes = "hello".getBytes(UTF_8); - buffer.writeIndex = 6; - buffer.readIndex = 6; + buffer.setWriteIndex(6); + buffer.setReadIndex(6); boolean written = buffer.put(bytes); assertTrue(written); - assertArrayEquals(new byte[]{101, 108, 108, 111, 0, 0, 0, 5, 104}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 14, 0, 0, 0, 12, 101, 108, 108, 111, 0, 0, 0, 5, 104}, buffer.data.array()); assertArrayEquals(bytes, buffer.get()); } @@ -63,11 +66,11 @@ class CircularByteBufferTest { void testWrapAroundPutLenBeforeWrap() { CircularByteBuffer buffer = new CircularByteBuffer(9); byte[] bytes = "hello".getBytes(UTF_8); - buffer.writeIndex = 7; - buffer.readIndex = 7; + buffer.setWriteIndex(7); + buffer.setReadIndex(7); boolean written = buffer.put(bytes); assertTrue(written); - assertArrayEquals(new byte[]{104, 101, 108, 108, 111, 0, 0, 0, 5}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 15, 0, 0, 0, 13, 104, 101, 108, 108, 111, 0, 0, 0, 5}, buffer.data.array()); assertArrayEquals(bytes, buffer.get()); } @@ -75,11 +78,11 @@ class CircularByteBufferTest { void testWrapAroundPutLenSplitBeforeWrap() { CircularByteBuffer buffer = new CircularByteBuffer(9); byte[] bytes = "hello".getBytes(UTF_8); - buffer.writeIndex = 8; - buffer.readIndex = 8; + buffer.setWriteIndex(8); + buffer.setReadIndex(8); boolean written = buffer.put(bytes); assertTrue(written); - assertArrayEquals(new byte[]{5, 104, 101, 108, 108, 111, 0, 0, 0}, buffer.data.array()); + assertArrayEquals(new byte[]{0, 0, 0, 16, 0, 0, 0, 14, 5, 104, 101, 108, 108, 111, 0, 0, 0}, buffer.data.array()); assertArrayEquals(bytes, buffer.get()); } @@ -96,28 +99,28 @@ class CircularByteBufferTest { @Test void testFreeSpaceReclaimed() { CircularByteBuffer buffer = new CircularByteBuffer(9); - assertEquals(0, buffer.readIndex); - assertEquals(0, buffer.writeIndex); + assertEquals(0, buffer.getReadIndex()); + assertEquals(0, buffer.getWriteIndex()); byte[] bytes = "hello".getBytes(UTF_8); boolean written1 = buffer.put(bytes); assertTrue(written1); - assertEquals(0, buffer.readIndex); - assertEquals(7, buffer.writeIndex); + assertEquals(0, buffer.getReadIndex()); + assertEquals(7, buffer.getWriteIndex()); assertArrayEquals(bytes, buffer.get()); - assertEquals(7, buffer.readIndex); - assertEquals(7, buffer.writeIndex); + assertEquals(7, buffer.getReadIndex()); + assertEquals(7, buffer.getWriteIndex()); boolean written2 = buffer.put(bytes); assertTrue(written2); // the read has freed space - assertEquals(7, buffer.readIndex); - assertEquals(5, buffer.writeIndex); + assertEquals(7, buffer.getReadIndex()); + assertEquals(5, buffer.getWriteIndex()); assertArrayEquals(bytes, buffer.get()); - assertEquals(5, buffer.readIndex); - assertEquals(5, buffer.writeIndex); + assertEquals(5, buffer.getReadIndex()); + assertEquals(5, buffer.getWriteIndex()); } diff --git a/lib/src/test/java/com/github/shautvast/exceptional/RingBufferTest.java b/lib/src/test/java/com/github/shautvast/exceptional/RingBufferTest.java new file mode 100644 index 0000000..897c6ea --- /dev/null +++ b/lib/src/test/java/com/github/shautvast/exceptional/RingBufferTest.java @@ -0,0 +1,24 @@ +package com.github.shautvast.exceptional; + + +import org.junit.jupiter.api.Test; + +import java.lang.foreign.MemorySegment; +import java.nio.charset.StandardCharsets; + +// TODO scheduled for demolition +class RingBufferTest { + + @Test + void testWriteAndRead() { + RingBuffer ringBuffer = new RingBuffer(MemorySegment.ofArray(new byte[16])); + ringBuffer.startReader(x -> System.out.println("read " + new String(x, StandardCharsets.UTF_8))); + for (int i = 0; i < 10; i++) { + System.out.println("put " + i + " in ring buffer"); + byte[] testdata = ("test" + i).getBytes(StandardCharsets.UTF_8); + ringBuffer.write(testdata); + } + ringBuffer.drain(); + + } +}