diff --git a/src/main/java/co/nstant/in/cbor/CborDecoder.java b/src/main/java/co/nstant/in/cbor/CborDecoder.java index 0c920296..039fe037 100644 --- a/src/main/java/co/nstant/in/cbor/CborDecoder.java +++ b/src/main/java/co/nstant/in/cbor/CborDecoder.java @@ -158,7 +158,6 @@ public DataItem decodeNext() throws CborException { return next; } } - case INVALID: default: throw new CborException("Not implemented major type " + symbol); } diff --git a/src/main/java/co/nstant/in/cbor/CborEncoder.java b/src/main/java/co/nstant/in/cbor/CborEncoder.java index c158a713..f8eb30fb 100644 --- a/src/main/java/co/nstant/in/cbor/CborEncoder.java +++ b/src/main/java/co/nstant/in/cbor/CborEncoder.java @@ -1,17 +1,11 @@ package co.nstant.in.cbor; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Objects; -import co.nstant.in.cbor.encoder.ArrayEncoder; -import co.nstant.in.cbor.encoder.ByteStringEncoder; -import co.nstant.in.cbor.encoder.MapEncoder; -import co.nstant.in.cbor.encoder.NegativeIntegerEncoder; -import co.nstant.in.cbor.encoder.SpecialEncoder; -import co.nstant.in.cbor.encoder.TagEncoder; -import co.nstant.in.cbor.encoder.UnicodeStringEncoder; -import co.nstant.in.cbor.encoder.UnsignedIntegerEncoder; import co.nstant.in.cbor.model.Array; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; @@ -28,32 +22,17 @@ */ public class CborEncoder { - private final UnsignedIntegerEncoder unsignedIntegerEncoder; - private final NegativeIntegerEncoder negativeIntegerEncoder; - private final ByteStringEncoder byteStringEncoder; - private final UnicodeStringEncoder unicodeStringEncoder; - private final ArrayEncoder arrayEncoder; - private final MapEncoder mapEncoder; - private final TagEncoder tagEncoder; - private final SpecialEncoder specialEncoder; - private boolean canonical = true; + private CborOutputStream cborOutputStream; /** * Initialize a new encoder which writes the binary encoded data to an * {@link OutputStream}. - * + * * @param outputStream the {@link OutputStream} to write the encoded data to */ public CborEncoder(OutputStream outputStream) { Objects.requireNonNull(outputStream); - unsignedIntegerEncoder = new UnsignedIntegerEncoder(this, outputStream); - negativeIntegerEncoder = new NegativeIntegerEncoder(this, outputStream); - byteStringEncoder = new ByteStringEncoder(this, outputStream); - unicodeStringEncoder = new UnicodeStringEncoder(this, outputStream); - arrayEncoder = new ArrayEncoder(this, outputStream); - mapEncoder = new MapEncoder(this, outputStream); - tagEncoder = new TagEncoder(this, outputStream); - specialEncoder = new SpecialEncoder(this, outputStream); + cborOutputStream = new CborOutputStream(outputStream); } /** @@ -78,52 +57,57 @@ public void encode(List dataItems) throws CborException { * an problem with the {@link OutputStream}. */ public void encode(DataItem dataItem) throws CborException { - if (dataItem == null) { - dataItem = SimpleValue.NULL; - } - - if (dataItem.hasTag()) { - Tag tagDi = dataItem.getTag(); - encode(tagDi); - } - - switch (dataItem.getMajorType()) { - case UNSIGNED_INTEGER: - unsignedIntegerEncoder.encode((UnsignedInteger) dataItem); - break; - case NEGATIVE_INTEGER: - negativeIntegerEncoder.encode((NegativeInteger) dataItem); - break; - case BYTE_STRING: - byteStringEncoder.encode((ByteString) dataItem); - break; - case UNICODE_STRING: - unicodeStringEncoder.encode((UnicodeString) dataItem); - break; - case ARRAY: - arrayEncoder.encode((Array) dataItem); - break; - case MAP: - mapEncoder.encode((Map) dataItem); - break; - case SPECIAL: - specialEncoder.encode((Special) dataItem); - break; - case TAG: - tagEncoder.encode((Tag) dataItem); - break; - default: - throw new CborException("Unknown major type"); + try { + cborOutputStream.writeDataItem(dataItem); + } catch (IOException ioException) { + throw new CborException(ioException); } } public boolean isCanonical() { - return canonical; + return cborOutputStream.isCanonical(); } public CborEncoder nonCanonical() { - canonical = false; + cborOutputStream.setCanonical(false); return this; } + /** + * Encode a list of {@link DataItem}s, also known as a stream, to a byte array. + * + * @param dataItems a list of {@link DataItem}s + */ + public static byte[] encodeToBytes(List dataItems) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CborOutputStream cborOutputStream = new CborOutputStream(byteArrayOutputStream); + try { + for (DataItem dataItem : dataItems) { + cborOutputStream.writeDataItem(dataItem); + } + } catch (IOException ioException) { + // A ByteArrayOutputStream does not actually throw an IOException. + throw new AssertionError(ioException); + } + return byteArrayOutputStream.toByteArray(); + } + + /** + * Encode a single {@link DataItem} to a byte array. + * + * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a + * {@link SimpleValue} NULL value. + */ + public static byte[] encodeToBytes(DataItem dataItem) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CborOutputStream cborOutputStream = new CborOutputStream(byteArrayOutputStream); + try { + cborOutputStream.writeDataItem(dataItem); + } catch (IOException ioException) { + // A ByteArrayOutputStream does not actually throw an IOException. + throw new AssertionError(ioException); + } + return byteArrayOutputStream.toByteArray(); + } + } diff --git a/src/main/java/co/nstant/in/cbor/CborOutputStream.java b/src/main/java/co/nstant/in/cbor/CborOutputStream.java new file mode 100644 index 00000000..deb5e3a1 --- /dev/null +++ b/src/main/java/co/nstant/in/cbor/CborOutputStream.java @@ -0,0 +1,422 @@ +package co.nstant.in.cbor; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.TreeMap; + +import co.nstant.in.cbor.model.AdditionalInformation; +import co.nstant.in.cbor.model.Array; +import co.nstant.in.cbor.model.ByteString; +import co.nstant.in.cbor.model.DataItem; +import co.nstant.in.cbor.model.DoublePrecisionFloat; +import co.nstant.in.cbor.model.HalfPrecisionFloat; +import co.nstant.in.cbor.model.MajorType; +import co.nstant.in.cbor.model.Map; +import co.nstant.in.cbor.model.NegativeInteger; +import co.nstant.in.cbor.model.SimpleValue; +import co.nstant.in.cbor.model.SimpleValueType; +import co.nstant.in.cbor.model.SinglePrecisionFloat; +import co.nstant.in.cbor.model.Special; +import co.nstant.in.cbor.model.Tag; +import co.nstant.in.cbor.model.UnicodeString; +import co.nstant.in.cbor.model.UnsignedInteger; + +class CborOutputStream extends OutputStream { + + private static final BigInteger MINUS_ONE = BigInteger.valueOf(-1); + private static final BigInteger UINT64_MAX_PLUS_ONE = new BigInteger("18446744073709551616"); + + private final OutputStream outputStream; + private boolean canonical = true; + + public CborOutputStream(OutputStream outputStream) { + this.outputStream = outputStream; + } + + public boolean isCanonical() { + return canonical; + } + + public void setCanonical(boolean canonical) { + this.canonical = canonical; + } + + public void writeDataItem(DataItem dataItem) throws IOException { + if (dataItem == null) { + dataItem = SimpleValue.NULL; + } + + if (dataItem.hasTag()) { + writeDataItem(dataItem.getTag()); + } + + switch (dataItem.getMajorType()) { + case UNSIGNED_INTEGER: + writeUnsignedInteger((UnsignedInteger) dataItem); + break; + case NEGATIVE_INTEGER: + writeNegativeInteger((NegativeInteger) dataItem); + break; + case BYTE_STRING: + writeByteString((ByteString) dataItem); + break; + case UNICODE_STRING: + writeUnicodeString((UnicodeString) dataItem); + break; + case ARRAY: + writeArray((Array) dataItem); + break; + case MAP: + writeMap((Map) dataItem); + break; + case SPECIAL: + writeSpecial((Special) dataItem); + break; + case TAG: + writeTag((Tag) dataItem); + break; + default: + throw new AssertionError("Unknown major type"); + } + } + + private void writeUnsignedInteger(UnsignedInteger dataItem) throws IOException { + writeType(MajorType.UNSIGNED_INTEGER, dataItem.getValue()); + } + + private void writeNegativeInteger(NegativeInteger dataItem) throws IOException { + writeType(MajorType.NEGATIVE_INTEGER, MINUS_ONE.subtract(dataItem.getValue()).abs()); + } + + private void writeByteString(ByteString byteString) throws IOException { + byte[] bytes = byteString.getBytes(); + if (byteString.isChunked()) { + writeIndefiniteLengthType(MajorType.BYTE_STRING); + if (bytes == null) { + return; + } + } else if (bytes == null) { + writeSpecial(SimpleValue.NULL); + return; + } + writeType(MajorType.BYTE_STRING, bytes.length); + write(bytes); + } + + private void writeUnicodeString(UnicodeString dataItem) throws IOException { + String string = dataItem.getString(); + if (dataItem.isChunked()) { + writeIndefiniteLengthType(MajorType.UNICODE_STRING); + if (string == null) { + return; + } + } else if (string == null) { + writeSpecial(SimpleValue.NULL); + return; + } + byte[] bytes = string.getBytes(StandardCharsets.UTF_8); + writeType(MajorType.UNICODE_STRING, bytes.length); + write(bytes); + } + + private void writeArray(Array array) throws IOException { + List dataItems = array.getDataItems(); + if (array.isChunked()) { + writeIndefiniteLengthType(MajorType.ARRAY); + } else { + writeType(MajorType.ARRAY, dataItems.size()); + } + for (DataItem dataItem : dataItems) { + writeDataItem(dataItem); + } + } + + private void writeMap(Map map) throws IOException { + Collection keys = map.getKeys(); + + if (map.isChunked()) { + writeIndefiniteLengthType(MajorType.MAP); + } else { + writeType(MajorType.MAP, keys.size()); + } + + if (keys.isEmpty()) { + return; + } + + if (map.isChunked()) { + writeNonCanonicalMap(map); + writeSpecial(SimpleValue.BREAK); + } else { + if (canonical) { + writeCanonicalMap(map); + } else { + writeNonCanonicalMap(map); + } + } + } + + private void writeNonCanonicalMap(Map map) throws IOException { + for (DataItem key : map.getKeys()) { + writeDataItem(key); + writeDataItem(map.get(key)); + } + } + + /** + * Writes the map in the canonical CBOR format. + * + *

From RFC 7049: + * + *

The keys in every map must be sorted lowest value to highest. Sorting is performed on the + * bytes of the representation of the key data items without paying attention to the 3/5 bit + * splitting for major types. (Note that this rule allows maps that have keys of different + * types, even though that is probably a bad practice that could lead to errors in some + * canonicalization implementations.) The sorting rules are: + * + *

If two keys have different lengths, the shorter one sorts earlier; + * + *

If two keys have the same length, the one with the lower value in (byte-wise) lexical order + * sorts earlier. + */ + private void writeCanonicalMap(Map map) throws IOException { + TreeMap sortedMap = new TreeMap<>(new Comparator() { + + @Override + public int compare(byte[] o1, byte[] o2) { + if (o1.length < o2.length) { + return -1; + } + if (o1.length > o2.length) { + return 1; + } + for (int i = 0; i < o1.length; i++) { + if (o1[i] < o2[i]) { + return -1; + } + if (o1[i] > o2[i]) { + return 1; + } + } + return 0; + } + + }); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CborOutputStream cborOutputStream = new CborOutputStream(byteArrayOutputStream); + for (DataItem key : map.getKeys()) { + // Key + cborOutputStream.writeDataItem(key); + byte[] keyBytes = byteArrayOutputStream.toByteArray(); + byteArrayOutputStream.reset(); + // Value + cborOutputStream.writeDataItem(map.get(key)); + byte[] valueBytes = byteArrayOutputStream.toByteArray(); + byteArrayOutputStream.reset(); + sortedMap.put(keyBytes, valueBytes); + } + for (java.util.Map.Entry entry : sortedMap.entrySet()) { + write(entry.getKey()); + write(entry.getValue()); + } + } + + void writeSpecial(Special dataItem) throws IOException{ + switch (dataItem.getSpecialType()) { + case BREAK: + write((7 << 5) | 31); + break; + case SIMPLE_VALUE: + SimpleValue simpleValue = (SimpleValue) dataItem; + switch (simpleValue.getSimpleValueType()) { + case FALSE: + case NULL: + case TRUE: + case UNDEFINED: + SimpleValueType type = simpleValue.getSimpleValueType(); + write((7 << 5) | type.getValue()); + break; + case UNALLOCATED: + write((7 << 5) | simpleValue.getValue()); + break; + case RESERVED: + break; + } + break; + case IEEE_754_HALF_PRECISION_FLOAT: + writeHalfPrecisionFloat((HalfPrecisionFloat) dataItem); + break; + case IEEE_754_SINGLE_PRECISION_FLOAT: + writeSinglePrecisionFloat((SinglePrecisionFloat) dataItem); + break; + case IEEE_754_DOUBLE_PRECISION_FLOAT: + writeDoublePrecisionFloat((DoublePrecisionFloat) dataItem); + break; + case SIMPLE_VALUE_NEXT_BYTE: + SimpleValue simpleValueNextByte = (SimpleValue) dataItem; + writeBytes((byte) ((7 << 5) | 24), (byte) simpleValueNextByte.getValue()); + break; + default: + throw new AssertionError("Unknown special value type"); + } + } + + private void writeHalfPrecisionFloat(HalfPrecisionFloat dataItem) throws IOException { + int bits = fromFloat(dataItem.getValue()); + writeBytes((byte) ((7 << 5) | 25), (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); + } + + /** + * @param fval the float value + * @return all higher 16 bits as 0 for all results + * @see Stack Overflow + */ + private static int fromFloat(float fval) { + int fbits = Float.floatToIntBits(fval); + int sign = fbits >>> 16 & 0x8000; // sign only + int val = 0x1000 + fbits & 0x7fffffff; // rounded value + + if (val >= 0x47800000) // might be or become NaN/Inf + { // avoid Inf due to rounding + if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become + // NaN/Inf + if (val < 0x7f800000) {// was value but too large + return sign | 0x7c00; // make it +/-Inf + } + return sign | 0x7c00 | // remains +/-Inf or NaN + (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; // tmp exp for subnormal calc + return sign | ((fbits & 0x7fffff | 0x800000) // add subnormal bit + + (0x800000 >>> val - 102) // round depending on cut off + >>> 126 - val); // div by 2^(1-(exp-127+15)) and >> 13 | exp=0 + } + + public void writeSinglePrecisionFloat(SinglePrecisionFloat dataItem) throws IOException { + int bits = Float.floatToRawIntBits(dataItem.getValue()); + writeBytes((byte) ((7 << 5) | 26), (byte) ((bits >> 24) & 0xFF), (byte) ((bits >> 16) & 0xFF), + (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); + } + + public void writeDoublePrecisionFloat(DoublePrecisionFloat dataItem) throws IOException { + long bits = Double.doubleToRawLongBits(dataItem.getValue()); + writeBytes((byte) ((7 << 5) | 27), (byte) ((bits >> 56) & 0xFF), (byte) ((bits >> 48) & 0xFF), + (byte) ((bits >> 40) & 0xFF), (byte) ((bits >> 32) & 0xFF), (byte) ((bits >> 24) & 0xFF), + (byte) ((bits >> 16) & 0xFF), (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); + } + + private void writeTag(Tag tag) throws IOException { + writeType(MajorType.TAG, tag.getValue()); + } + + private void writeIndefiniteLengthType(MajorType majorType) throws IOException { + int symbol = majorType.getValue() << 5; + symbol |= AdditionalInformation.INDEFINITE.getValue(); + outputStream.write(symbol); + } + + private void writeType(MajorType majorType, long length) throws IOException { + int symbol = majorType.getValue() << 5; + if (length <= 23L) { + write((byte) (symbol | length)); + } else if (length <= 255L) { + symbol |= AdditionalInformation.ONE_BYTE.getValue(); + writeBytes((byte) symbol, (byte) length); + } else if (length <= 65535L) { + symbol |= AdditionalInformation.TWO_BYTES.getValue(); + writeBytes((byte) symbol, (byte) (length >> 8), (byte) (length & 0xFF)); + } else if (length <= 4294967295L) { + symbol |= AdditionalInformation.FOUR_BYTES.getValue(); + writeBytes((byte) symbol, (byte) ((length >> 24) & 0xFF), (byte) ((length >> 16) & 0xFF), + (byte) ((length >> 8) & 0xFF), (byte) (length & 0xFF)); + } else { + symbol |= AdditionalInformation.EIGHT_BYTES.getValue(); + writeBytes((byte) symbol, (byte) ((length >> 56) & 0xFF), (byte) ((length >> 48) & 0xFF), + (byte) ((length >> 40) & 0xFF), (byte) ((length >> 32) & 0xFF), (byte) ((length >> 24) & 0xFF), + (byte) ((length >> 16) & 0xFF), (byte) ((length >> 8) & 0xFF), (byte) (length & 0xFF)); + } + } + + private void writeType(MajorType majorType, BigInteger length) throws IOException { + boolean negative = majorType == MajorType.NEGATIVE_INTEGER; + int symbol = majorType.getValue() << 5; + if (length.compareTo(BigInteger.valueOf(24)) < 0) { + write(symbol | length.intValue()); + } else if (length.compareTo(BigInteger.valueOf(256)) < 0) { + symbol |= AdditionalInformation.ONE_BYTE.getValue(); + writeBytes((byte) symbol, (byte) length.intValue()); + } else if (length.compareTo(BigInteger.valueOf(65536L)) < 0) { + symbol |= AdditionalInformation.TWO_BYTES.getValue(); + long twoByteValue = length.longValue(); + writeBytes((byte) symbol, (byte) (twoByteValue >> 8), (byte) (twoByteValue & 0xFF)); + } else if (length.compareTo(BigInteger.valueOf(4294967296L)) < 0) { + symbol |= AdditionalInformation.FOUR_BYTES.getValue(); + long fourByteValue = length.longValue(); + writeBytes((byte) symbol, (byte) ((fourByteValue >> 24) & 0xFF), (byte) ((fourByteValue >> 16) & 0xFF), + (byte) ((fourByteValue >> 8) & 0xFF), (byte) (fourByteValue & 0xFF)); + } else if (length.compareTo(UINT64_MAX_PLUS_ONE) < 0) { + symbol |= AdditionalInformation.EIGHT_BYTES.getValue(); + BigInteger mask = BigInteger.valueOf(0xFF); + writeBytes((byte) symbol, length.shiftRight(56).and(mask).byteValue(), + length.shiftRight(48).and(mask).byteValue(), length.shiftRight(40).and(mask).byteValue(), + length.shiftRight(32).and(mask).byteValue(), length.shiftRight(24).and(mask).byteValue(), + length.shiftRight(16).and(mask).byteValue(), length.shiftRight(8).and(mask).byteValue(), + length.and(mask).byteValue()); + } else { + if (negative) { + writeType(MajorType.TAG, 3); + } else { + writeType(MajorType.TAG, 2); + } + byte[] bytes = length.toByteArray(); + writeType(MajorType.BYTE_STRING, bytes.length); + write(bytes); + } + } + + private void writeBytes(byte... bytes) throws IOException { + outputStream.write(bytes); + } + + @Override + public void write(int b) throws IOException { + outputStream.write(b); + } + + @Override + public void write(byte[] b) throws IOException { + outputStream.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + outputStream.write(b, off, len); + } + + @Override + public void flush() throws IOException { + outputStream.flush(); + } + + @Override + public void close() throws IOException { + outputStream.close(); + } + +} diff --git a/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java b/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java index 2b227b5f..3f662863 100644 --- a/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java +++ b/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java @@ -1,14 +1,13 @@ package co.nstant.in.cbor.builder; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; +import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.decoder.HalfPrecisionFloatDecoder; -import co.nstant.in.cbor.encoder.HalfPrecisionFloatEncoder; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.DoublePrecisionFloat; @@ -86,10 +85,7 @@ protected Tag tag(long value) { private boolean isHalfPrecisionEnough(float value) { try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - HalfPrecisionFloatEncoder encoder = getHalfPrecisionFloatEncoder(outputStream); - encoder.encode(new HalfPrecisionFloat(value)); - byte[] bytes = outputStream.toByteArray(); + byte[] bytes = new HalfPrecisionFloat(value).encodeToBytes(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); HalfPrecisionFloatDecoder decoder = getHalfPrecisionFloatDecoder(inputStream); if (inputStream.read() == -1) { // to skip type byte @@ -102,10 +98,6 @@ private boolean isHalfPrecisionEnough(float value) { } } - protected HalfPrecisionFloatEncoder getHalfPrecisionFloatEncoder(OutputStream outputStream) { - return new HalfPrecisionFloatEncoder(null, outputStream); - } - protected HalfPrecisionFloatDecoder getHalfPrecisionFloatDecoder(InputStream inputStream) { return new HalfPrecisionFloatDecoder(null, inputStream); } diff --git a/src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java b/src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java index bab71e74..7ca4bf13 100644 --- a/src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java +++ b/src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java @@ -51,7 +51,6 @@ public Special decode(int initialByte) throws CborException { return doublePrecisionFloatDecoder.decode(initialByte); case SIMPLE_VALUE_NEXT_BYTE: return new SimpleValue(nextSymbol()); - case UNALLOCATED: default: throw new CborException("Not implemented"); } diff --git a/src/main/java/co/nstant/in/cbor/encoder/AbstractEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/AbstractEncoder.java deleted file mode 100644 index 25d0d19e..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/AbstractEncoder.java +++ /dev/null @@ -1,109 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.IOException; -import java.io.OutputStream; -import java.math.BigInteger; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.AdditionalInformation; -import co.nstant.in.cbor.model.ByteString; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.Tag; - -public abstract class AbstractEncoder { - - private final OutputStream outputStream; - protected final CborEncoder encoder; - - public AbstractEncoder(CborEncoder encoder, OutputStream outputStream) { - this.encoder = encoder; - this.outputStream = outputStream; - } - - public abstract void encode(T dataItem) throws CborException; - - protected void encodeTypeChunked(MajorType majorType) throws CborException { - int symbol = majorType.getValue() << 5; - symbol |= AdditionalInformation.INDEFINITE.getValue(); - try { - outputStream.write(symbol); - } catch (IOException ioException) { - throw new CborException(ioException); - } - } - - protected void encodeTypeAndLength(MajorType majorType, long length) throws CborException { - int symbol = majorType.getValue() << 5; - if (length <= 23L) { - write((byte) (symbol | length)); - } else if (length <= 255L) { - symbol |= AdditionalInformation.ONE_BYTE.getValue(); - write((byte) symbol, (byte) length); - } else if (length <= 65535L) { - symbol |= AdditionalInformation.TWO_BYTES.getValue(); - write((byte) symbol, (byte) (length >> 8), (byte) (length & 0xFF)); - } else if (length <= 4294967295L) { - symbol |= AdditionalInformation.FOUR_BYTES.getValue(); - write((byte) symbol, (byte) ((length >> 24) & 0xFF), (byte) ((length >> 16) & 0xFF), - (byte) ((length >> 8) & 0xFF), (byte) (length & 0xFF)); - } else { - symbol |= AdditionalInformation.EIGHT_BYTES.getValue(); - write((byte) symbol, (byte) ((length >> 56) & 0xFF), (byte) ((length >> 48) & 0xFF), - (byte) ((length >> 40) & 0xFF), (byte) ((length >> 32) & 0xFF), (byte) ((length >> 24) & 0xFF), - (byte) ((length >> 16) & 0xFF), (byte) ((length >> 8) & 0xFF), (byte) (length & 0xFF)); - } - } - - protected void encodeTypeAndLength(MajorType majorType, BigInteger length) throws CborException { - boolean negative = majorType == MajorType.NEGATIVE_INTEGER; - int symbol = majorType.getValue() << 5; - if (length.compareTo(BigInteger.valueOf(24)) < 0) { - write(symbol | length.intValue()); - } else if (length.compareTo(BigInteger.valueOf(256)) < 0) { - symbol |= AdditionalInformation.ONE_BYTE.getValue(); - write((byte) symbol, (byte) length.intValue()); - } else if (length.compareTo(BigInteger.valueOf(65536L)) < 0) { - symbol |= AdditionalInformation.TWO_BYTES.getValue(); - long twoByteValue = length.longValue(); - write((byte) symbol, (byte) (twoByteValue >> 8), (byte) (twoByteValue & 0xFF)); - } else if (length.compareTo(BigInteger.valueOf(4294967296L)) < 0) { - symbol |= AdditionalInformation.FOUR_BYTES.getValue(); - long fourByteValue = length.longValue(); - write((byte) symbol, (byte) ((fourByteValue >> 24) & 0xFF), (byte) ((fourByteValue >> 16) & 0xFF), - (byte) ((fourByteValue >> 8) & 0xFF), (byte) (fourByteValue & 0xFF)); - } else if (length.compareTo(new BigInteger("18446744073709551616")) < 0) { - symbol |= AdditionalInformation.EIGHT_BYTES.getValue(); - BigInteger mask = BigInteger.valueOf(0xFF); - write((byte) symbol, length.shiftRight(56).and(mask).byteValue(), - length.shiftRight(48).and(mask).byteValue(), length.shiftRight(40).and(mask).byteValue(), - length.shiftRight(32).and(mask).byteValue(), length.shiftRight(24).and(mask).byteValue(), - length.shiftRight(16).and(mask).byteValue(), length.shiftRight(8).and(mask).byteValue(), - length.and(mask).byteValue()); - } else { - if (negative) { - encoder.encode(new Tag(3)); - } else { - encoder.encode(new Tag(2)); - } - encoder.encode(new ByteString(length.toByteArray())); - } - } - - protected void write(int b) throws CborException { - try { - outputStream.write(b); - } catch (IOException ioException) { - throw new CborException(ioException); - } - } - - protected void write(byte... bytes) throws CborException { - try { - outputStream.write(bytes); - } catch (IOException ioException) { - throw new CborException(ioException); - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/ArrayEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/ArrayEncoder.java deleted file mode 100644 index ae0ddbe8..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/ArrayEncoder.java +++ /dev/null @@ -1,31 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; -import java.util.List; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.Array; -import co.nstant.in.cbor.model.DataItem; -import co.nstant.in.cbor.model.MajorType; - -public class ArrayEncoder extends AbstractEncoder { - - public ArrayEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(Array array) throws CborException { - List dataItems = array.getDataItems(); - if (array.isChunked()) { - encodeTypeChunked(MajorType.ARRAY); - } else { - encodeTypeAndLength(MajorType.ARRAY, dataItems.size()); - } - for (DataItem dataItem : dataItems) { - encoder.encode(dataItem); - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/ByteStringEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/ByteStringEncoder.java deleted file mode 100644 index 6c1ca198..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/ByteStringEncoder.java +++ /dev/null @@ -1,33 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.ByteString; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.SimpleValue; - -public class ByteStringEncoder extends AbstractEncoder { - - public ByteStringEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(ByteString byteString) throws CborException { - byte[] bytes = byteString.getBytes(); - if (byteString.isChunked()) { - encodeTypeChunked(MajorType.BYTE_STRING); - if (bytes != null) { - encode(new ByteString(bytes)); - } - } else if (bytes == null) { - encoder.encode(SimpleValue.NULL); - } else { - encodeTypeAndLength(MajorType.BYTE_STRING, bytes.length); - write(bytes); - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/DoublePrecisionFloatEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/DoublePrecisionFloatEncoder.java deleted file mode 100644 index 26b84ec3..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/DoublePrecisionFloatEncoder.java +++ /dev/null @@ -1,23 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.DoublePrecisionFloat; - -public class DoublePrecisionFloatEncoder extends AbstractEncoder { - - public DoublePrecisionFloatEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(DoublePrecisionFloat dataItem) throws CborException { - long bits = Double.doubleToRawLongBits(dataItem.getValue()); - write((byte) ((7 << 5) | 27), (byte) ((bits >> 56) & 0xFF), (byte) ((bits >> 48) & 0xFF), - (byte) ((bits >> 40) & 0xFF), (byte) ((bits >> 32) & 0xFF), (byte) ((bits >> 24) & 0xFF), - (byte) ((bits >> 16) & 0xFF), (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/HalfPrecisionFloatEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/HalfPrecisionFloatEncoder.java deleted file mode 100644 index d805c99a..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/HalfPrecisionFloatEncoder.java +++ /dev/null @@ -1,56 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.HalfPrecisionFloat; - -public class HalfPrecisionFloatEncoder extends AbstractEncoder { - - public HalfPrecisionFloatEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(HalfPrecisionFloat dataItem) throws CborException { - int bits = fromFloat(dataItem.getValue()); - write((byte) ((7 << 5) | 25), (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); - } - - /** - * @param fval the float value - * @return all higher 16 bits as 0 for all results - * @see Stack Overflow - */ - public static int fromFloat(float fval) { - int fbits = Float.floatToIntBits(fval); - int sign = fbits >>> 16 & 0x8000; // sign only - int val = 0x1000 + fbits & 0x7fffffff; // rounded value - - if (val >= 0x47800000) // might be or become NaN/Inf - { // avoid Inf due to rounding - if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become - // NaN/Inf - if (val < 0x7f800000) {// was value but too large - return sign | 0x7c00; // make it +/-Inf - } - return sign | 0x7c00 | // remains +/-Inf or NaN - (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; // tmp exp for subnormal calc - return sign | ((fbits & 0x7fffff | 0x800000) // add subnormal bit - + (0x800000 >>> val - 102) // round depending on cut off - >>> 126 - val); // div by 2^(1-(exp-127+15)) and >> 13 | exp=0 - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/MapEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/MapEncoder.java deleted file mode 100644 index c54d0236..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/MapEncoder.java +++ /dev/null @@ -1,116 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; -import java.util.Collection; -import java.util.Comparator; -import java.util.TreeMap; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.DataItem; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.Map; -import co.nstant.in.cbor.model.SimpleValue; - -public class MapEncoder extends AbstractEncoder { - - public MapEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(Map map) throws CborException { - Collection keys = map.getKeys(); - - if (map.isChunked()) { - encodeTypeChunked(MajorType.MAP); - } else { - encodeTypeAndLength(MajorType.MAP, keys.size()); - } - - if (keys.isEmpty()) { - return; - } - - if (map.isChunked()) { - encodeNonCanonical(map); - encoder.encode(SimpleValue.BREAK); - } else { - if (encoder.isCanonical()) { - encodeCanonical(map); - } else { - encodeNonCanonical(map); - } - } - } - - private void encodeNonCanonical(Map map) throws CborException { - for (DataItem key : map.getKeys()) { - encoder.encode(key); - encoder.encode(map.get(key)); - } - } - - private void encodeCanonical(Map map) throws CborException { - /** - * From https://tools.ietf.org/html/rfc7049#section-3.9 - * - * Canonical CBOR - * - * The keys in every map must be sorted lowest value to highest. Sorting is - * performed on the bytes of the representation of the key data items without - * paying attention to the 3/5 bit splitting for major types. (Note that this - * rule allows maps that have keys of different types, even though that is - * probably a bad practice that could lead to errors in some canonicalization - * implementations.) The sorting rules are: - * - * If two keys have different lengths, the shorter one sorts earlier; - * - * If two keys have the same length, the one with the lower value in (byte-wise) - * lexical order sorts earlier. - */ - - TreeMap sortedMap = new TreeMap<>(new Comparator() { - - @Override - public int compare(byte[] o1, byte[] o2) { - if (o1.length < o2.length) { - return -1; - } - if (o1.length > o2.length) { - return 1; - } - for (int i = 0; i < o1.length; i++) { - if (o1[i] < o2[i]) { - return -1; - } - if (o1[i] > o2[i]) { - return 1; - } - } - return 0; - } - - }); - - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CborEncoder e = new CborEncoder(byteArrayOutputStream); - for (DataItem key : map.getKeys()) { - // Key - e.encode(key); - byte[] keyBytes = byteArrayOutputStream.toByteArray(); - byteArrayOutputStream.reset(); - // Value - e.encode(map.get(key)); - byte[] valueBytes = byteArrayOutputStream.toByteArray(); - byteArrayOutputStream.reset(); - sortedMap.put(keyBytes, valueBytes); - } - for (java.util.Map.Entry entry : sortedMap.entrySet()) { - write(entry.getKey()); - write(entry.getValue()); - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/NegativeIntegerEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/NegativeIntegerEncoder.java deleted file mode 100644 index 9b9eb647..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/NegativeIntegerEncoder.java +++ /dev/null @@ -1,24 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; -import java.math.BigInteger; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.NegativeInteger; - -public class NegativeIntegerEncoder extends AbstractEncoder { - - private static final BigInteger MINUS_ONE = BigInteger.valueOf(-1); - - public NegativeIntegerEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(NegativeInteger dataItem) throws CborException { - encodeTypeAndLength(MajorType.NEGATIVE_INTEGER, MINUS_ONE.subtract(dataItem.getValue()).abs()); - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/SinglePrecisionFloatEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/SinglePrecisionFloatEncoder.java deleted file mode 100644 index 5c36b902..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/SinglePrecisionFloatEncoder.java +++ /dev/null @@ -1,22 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.SinglePrecisionFloat; - -public class SinglePrecisionFloatEncoder extends AbstractEncoder { - - public SinglePrecisionFloatEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(SinglePrecisionFloat dataItem) throws CborException { - int bits = Float.floatToRawIntBits(dataItem.getValue()); - write((byte) ((7 << 5) | 26), (byte) ((bits >> 24) & 0xFF), (byte) ((bits >> 16) & 0xFF), - (byte) ((bits >> 8) & 0xFF), (byte) ((bits >> 0) & 0xFF)); - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/SpecialEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/SpecialEncoder.java deleted file mode 100644 index e437dd3d..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/SpecialEncoder.java +++ /dev/null @@ -1,80 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.DoublePrecisionFloat; -import co.nstant.in.cbor.model.HalfPrecisionFloat; -import co.nstant.in.cbor.model.SimpleValue; -import co.nstant.in.cbor.model.SimpleValueType; -import co.nstant.in.cbor.model.SinglePrecisionFloat; -import co.nstant.in.cbor.model.Special; - -public class SpecialEncoder extends AbstractEncoder { - - private final HalfPrecisionFloatEncoder halfPrecisionFloatEncoder; - private final SinglePrecisionFloatEncoder singlePrecisionFloatEncoder; - private final DoublePrecisionFloatEncoder doublePrecisionFloatEncoder; - - public SpecialEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - halfPrecisionFloatEncoder = new HalfPrecisionFloatEncoder(encoder, outputStream); - singlePrecisionFloatEncoder = new SinglePrecisionFloatEncoder(encoder, outputStream); - doublePrecisionFloatEncoder = new DoublePrecisionFloatEncoder(encoder, outputStream); - } - - @Override - public void encode(Special dataItem) throws CborException { - switch (dataItem.getSpecialType()) { - case BREAK: - write((7 << 5) | 31); - break; - case SIMPLE_VALUE: - SimpleValue simpleValue = (SimpleValue) dataItem; - switch (simpleValue.getSimpleValueType()) { - case FALSE: - case NULL: - case TRUE: - case UNDEFINED: - SimpleValueType type = simpleValue.getSimpleValueType(); - write((7 << 5) | type.getValue()); - break; - case UNALLOCATED: - write((7 << 5) | simpleValue.getValue()); - break; - case RESERVED: - break; - } - break; - case UNALLOCATED: - throw new CborException("Unallocated special type"); - case IEEE_754_HALF_PRECISION_FLOAT: - if (!(dataItem instanceof HalfPrecisionFloat)) { - throw new CborException("Wrong data item type"); - } - halfPrecisionFloatEncoder.encode((HalfPrecisionFloat) dataItem); - break; - case IEEE_754_SINGLE_PRECISION_FLOAT: - if (!(dataItem instanceof SinglePrecisionFloat)) { - throw new CborException("Wrong data item type"); - } - singlePrecisionFloatEncoder.encode((SinglePrecisionFloat) dataItem); - break; - case IEEE_754_DOUBLE_PRECISION_FLOAT: - if (!(dataItem instanceof DoublePrecisionFloat)) { - throw new CborException("Wrong data item type"); - } - doublePrecisionFloatEncoder.encode((DoublePrecisionFloat) dataItem); - break; - case SIMPLE_VALUE_NEXT_BYTE: - if (!(dataItem instanceof SimpleValue)) { - throw new CborException("Wrong data item type"); - } - SimpleValue simpleValueNextByte = (SimpleValue) dataItem; - write((byte) ((7 << 5) | 24), (byte) simpleValueNextByte.getValue()); - break; - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/TagEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/TagEncoder.java deleted file mode 100644 index be9948e9..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/TagEncoder.java +++ /dev/null @@ -1,21 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.Tag; - -public class TagEncoder extends AbstractEncoder { - - public TagEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(Tag tag) throws CborException { - encodeTypeAndLength(MajorType.TAG, tag.getValue()); - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/UnicodeStringEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/UnicodeStringEncoder.java deleted file mode 100644 index ef769cbb..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/UnicodeStringEncoder.java +++ /dev/null @@ -1,36 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; -import java.nio.charset.StandardCharsets; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.SimpleValue; -import co.nstant.in.cbor.model.UnicodeString; - -public class UnicodeStringEncoder extends AbstractEncoder { - - public UnicodeStringEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(UnicodeString dataItem) throws CborException { - String string = dataItem.getString(); - if (dataItem.isChunked()) { - encodeTypeChunked(MajorType.UNICODE_STRING); - if (string != null) { - encode(new UnicodeString(string)); - } - } else if (string == null) { - encoder.encode(SimpleValue.NULL); - } else { - byte[] bytes; - bytes = string.getBytes(StandardCharsets.UTF_8); - encodeTypeAndLength(MajorType.UNICODE_STRING, bytes.length); - write(bytes); - } - } - -} diff --git a/src/main/java/co/nstant/in/cbor/encoder/UnsignedIntegerEncoder.java b/src/main/java/co/nstant/in/cbor/encoder/UnsignedIntegerEncoder.java deleted file mode 100644 index c7d08efa..00000000 --- a/src/main/java/co/nstant/in/cbor/encoder/UnsignedIntegerEncoder.java +++ /dev/null @@ -1,21 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import java.io.OutputStream; - -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.MajorType; -import co.nstant.in.cbor.model.UnsignedInteger; - -public class UnsignedIntegerEncoder extends AbstractEncoder { - - public UnsignedIntegerEncoder(CborEncoder encoder, OutputStream outputStream) { - super(encoder, outputStream); - } - - @Override - public void encode(UnsignedInteger dataItem) throws CborException { - encodeTypeAndLength(MajorType.UNSIGNED_INTEGER, dataItem.getValue()); - } - -} diff --git a/src/main/java/co/nstant/in/cbor/model/DataItem.java b/src/main/java/co/nstant/in/cbor/model/DataItem.java index 39248e50..511f3bea 100644 --- a/src/main/java/co/nstant/in/cbor/model/DataItem.java +++ b/src/main/java/co/nstant/in/cbor/model/DataItem.java @@ -2,6 +2,8 @@ import java.util.Objects; +import co.nstant.in.cbor.CborEncoder; + public class DataItem { private final MajorType majorType; @@ -73,4 +75,8 @@ public DataItem getOuterTaggable() { } return item; } + + public byte[] encodeToBytes() { + return CborEncoder.encodeToBytes(this); + } } diff --git a/src/main/java/co/nstant/in/cbor/model/MajorType.java b/src/main/java/co/nstant/in/cbor/model/MajorType.java index bc760741..b56b5085 100644 --- a/src/main/java/co/nstant/in/cbor/model/MajorType.java +++ b/src/main/java/co/nstant/in/cbor/model/MajorType.java @@ -1,8 +1,8 @@ package co.nstant.in.cbor.model; -public enum MajorType { +import co.nstant.in.cbor.CborException; - INVALID(-1), +public enum MajorType { /** * Major type 0: an unsigned integer. The 5-bit additional information is either @@ -98,7 +98,7 @@ public int getValue() { return value; } - public static MajorType ofByte(int b) { + public static MajorType ofByte(int b) throws CborException { switch (b >> 5) { case 0: return UNSIGNED_INTEGER; @@ -117,7 +117,7 @@ public static MajorType ofByte(int b) { case 7: return SPECIAL; default: - return INVALID; + throw new CborException("Not implemented major type " + b); } } diff --git a/src/main/java/co/nstant/in/cbor/model/SpecialType.java b/src/main/java/co/nstant/in/cbor/model/SpecialType.java index 2a96fb3f..5f4c8c1d 100644 --- a/src/main/java/co/nstant/in/cbor/model/SpecialType.java +++ b/src/main/java/co/nstant/in/cbor/model/SpecialType.java @@ -1,11 +1,13 @@ package co.nstant.in.cbor.model; +import co.nstant.in.cbor.CborException; + public enum SpecialType { SIMPLE_VALUE, SIMPLE_VALUE_NEXT_BYTE, IEEE_754_HALF_PRECISION_FLOAT, IEEE_754_SINGLE_PRECISION_FLOAT, - IEEE_754_DOUBLE_PRECISION_FLOAT, UNALLOCATED, BREAK; + IEEE_754_DOUBLE_PRECISION_FLOAT, BREAK; - public static SpecialType ofByte(int b) { + public static SpecialType ofByte(int b) throws CborException { switch (b & 31) { case 24: return SIMPLE_VALUE_NEXT_BYTE; @@ -18,7 +20,7 @@ public static SpecialType ofByte(int b) { case 28: case 29: case 30: - return UNALLOCATED; + throw new CborException("Not implemented special type " + b); case 31: return BREAK; default: diff --git a/src/test/java/co/nstant/in/cbor/CborEncoderTest.java b/src/test/java/co/nstant/in/cbor/CborEncoderTest.java index 060a90ed..a80a046c 100644 --- a/src/test/java/co/nstant/in/cbor/CborEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/CborEncoderTest.java @@ -1,8 +1,10 @@ package co.nstant.in.cbor; -import java.io.ByteArrayOutputStream; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.OutputStream; -import org.junit.Before; import org.junit.Test; import co.nstant.in.cbor.model.DataItem; @@ -10,24 +12,95 @@ public class CborEncoderTest { - private class InvalidDataItem extends DataItem { + private class Mock extends DataItem { - public InvalidDataItem() { - super(MajorType.INVALID); + public Mock(MajorType majorType) { + super(majorType); } } - private CborEncoder encoder; + @Test(expected = ClassCastException.class) + public void shouldExpectUnsignedIntegerImplementation() { + new Mock(MajorType.UNSIGNED_INTEGER).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectNegativeIntegerImplementation() { + new Mock(MajorType.NEGATIVE_INTEGER).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectByteStringImplementation() { + new Mock(MajorType.BYTE_STRING).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectUnicodeStringImplementation() { + new Mock(MajorType.UNICODE_STRING).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectArrayImplementation() { + new Mock(MajorType.ARRAY).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectMapImplementation() { + new Mock(MajorType.MAP).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectTagImplementation() { + new Mock(MajorType.TAG).encodeToBytes(); + } + + @Test(expected = ClassCastException.class) + public void shouldExpectSpecialImplementation() { + new Mock(MajorType.SPECIAL).encodeToBytes(); + } + + @Test(expected = CborException.class) + public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException { + new CborEncoder(new OutputStream() { + + private int counter = 0; + + @Override + public void write(int b) throws IOException { + if (++counter == 3) { + throw new IOException(); + } + } - @Before - public void setup() { - encoder = new CborEncoder(new ByteArrayOutputStream()); + }).encode(new CborBuilder().startString().add("string").end().build()); } @Test(expected = CborException.class) - public void shouldNotEncodeInvalidMajorType() throws CborException { - encoder.encode(new InvalidDataItem()); + public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException { + new CborEncoder(new OutputStream() { + + @Override + public void write(int b) throws IOException { + throw new IOException(); + } + + }).encode(new CborBuilder().startArray().add(1).end().build()); + } + + @Test + public void shallEncode32bit() { + byte[] bytes = CborEncoder.encodeToBytes(new CborBuilder().addTag(4294967296L).build()); + assertEquals(9, bytes.length); + assertEquals((byte) 0xdB, bytes[0]); + assertEquals((byte) 0x00, bytes[1]); + assertEquals((byte) 0x00, bytes[2]); + assertEquals((byte) 0x00, bytes[3]); + assertEquals((byte) 0x01, bytes[4]); + assertEquals((byte) 0x00, bytes[5]); + assertEquals((byte) 0x00, bytes[6]); + assertEquals((byte) 0x00, bytes[7]); + assertEquals((byte) 0x00, bytes[8]); } } diff --git a/src/test/java/co/nstant/in/cbor/DatemItemListenerTest.java b/src/test/java/co/nstant/in/cbor/DatemItemListenerTest.java index b215defd..363ea37a 100644 --- a/src/test/java/co/nstant/in/cbor/DatemItemListenerTest.java +++ b/src/test/java/co/nstant/in/cbor/DatemItemListenerTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import org.junit.Test; @@ -16,9 +15,8 @@ public class DatemItemListenerTest { @Test public void shouldDecodeZero() throws CborException { final int[] dataItems = new int[1]; - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - new CborEncoder(outputStream).encode(new CborBuilder().add(1234).build()); - new CborDecoder(new ByteArrayInputStream(outputStream.toByteArray())).decode(new DataItemListener() { + byte[] bytes = CborEncoder.encodeToBytes(new CborBuilder().add(1234).build()); + new CborDecoder(new ByteArrayInputStream(bytes)).decode(new DataItemListener() { @Override public void onDataItem(DataItem dataItem) { diff --git a/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java b/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java index 01a41941..1ef20ecb 100644 --- a/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java +++ b/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java @@ -7,7 +7,6 @@ import org.junit.Test; import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.encoder.HalfPrecisionFloatEncoder; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.HalfPrecisionFloat; import co.nstant.in.cbor.model.SinglePrecisionFloat; @@ -16,8 +15,6 @@ public class AbstractBuilderTest { private class TestBuilder extends AbstractBuilder { - private boolean encoderThrowsException = false; - public TestBuilder() { super(null); } @@ -30,24 +27,6 @@ public void testAddChunk() { addChunk(null); } - public void setEncoderThrowsException() { - encoderThrowsException = true; - } - - @Override - protected HalfPrecisionFloatEncoder getHalfPrecisionFloatEncoder(OutputStream outputStream) { - if (encoderThrowsException) { - return new HalfPrecisionFloatEncoder(null, outputStream) { - @Override - public void encode(HalfPrecisionFloat dataItem) throws CborException { - throw new CborException("test"); - } - }; - } else { - return super.getHalfPrecisionFloatEncoder(outputStream); - } - } - } @Test @@ -64,12 +43,4 @@ public void shouldThrowExceptionOnAddChunk() { new TestBuilder().testAddChunk(); } - @Test - public void IsHalfPrecisionEnoughShallReturnFalseOnException() { - TestBuilder builder = new TestBuilder(); - builder.setEncoderThrowsException(); - assertTrue(builder.testConvert(0.0f) instanceof SinglePrecisionFloat); - assertTrue(0.0f == ((SinglePrecisionFloat) builder.testConvert(0.0f)).getValue()); - } - } diff --git a/src/test/java/co/nstant/in/cbor/decoder/ByteStringDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/ByteStringDecoderTest.java index 7f0a1267..bf528091 100644 --- a/src/test/java/co/nstant/in/cbor/decoder/ByteStringDecoderTest.java +++ b/src/test/java/co/nstant/in/cbor/decoder/ByteStringDecoderTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertNotNull; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; @@ -21,11 +20,8 @@ public class ByteStringDecoderTest { @Test public void shouldDecodeChunkedByteString() throws CborException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(new CborBuilder().startByteString().add(new byte[] { '\0' }).add(new byte[] { 0x10 }) - .add(new byte[] { 0x13 }).end().build()); - byte[] encodedBytes = baos.toByteArray(); + byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().startByteString() + .add(new byte[] { '\0' }).add(new byte[] { 0x10 }).add(new byte[] { 0x13 }).end().build()); ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(bais); List dataItems = decoder.decode(); @@ -35,10 +31,7 @@ public void shouldDecodeChunkedByteString() throws CborException { @Test public void shouldDecodeByteString1K() throws CborException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(new CborBuilder().add(new byte[1024]).build()); - byte[] encodedBytes = baos.toByteArray(); + byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(new byte[1024]).build()); ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(bais); List dataItems = decoder.decode(); @@ -48,10 +41,7 @@ public void shouldDecodeByteString1K() throws CborException { @Test public void shouldDecodeByteString1M() throws CborException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(new CborBuilder().add(new byte[1024 * 1024]).build()); - byte[] encodedBytes = baos.toByteArray(); + byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(new byte[1024 * 1024]).build()); ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(bais); List dataItems = decoder.decode(); @@ -74,9 +64,7 @@ public void shouldTrowOnMissingBreak() throws CborException { public void decodingExample() throws CborException { byte bytes[] = { 0, 1, 2, 3 }; // Encode - ByteArrayOutputStream encodedStream = new ByteArrayOutputStream(); - new CborEncoder(encodedStream).encode(new ByteString(bytes)); - byte encodedBytes[] = encodedStream.toByteArray(); + byte[] encodedBytes = new ByteString(bytes).encodeToBytes(); // Decode ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(inputStream); diff --git a/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java index a4eaa1da..770e251c 100644 --- a/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java +++ b/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java @@ -8,7 +8,6 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -19,7 +18,6 @@ import co.nstant.in.cbor.CborDecoder; import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.encoder.AbstractEncoder; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.MajorType; import co.nstant.in.cbor.model.RationalNumber; @@ -84,10 +82,7 @@ public void shouldSetAutoDecodeLanguageTaggedStrings() { @Test(expected = CborException.class) public void shouldThrowOnRationalNumberDecode1() throws CborException { List items = new CborBuilder().addTag(30).add(true).build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -95,10 +90,7 @@ public void shouldThrowOnRationalNumberDecode1() throws CborException { @Test(expected = CborException.class) public void shouldThrowOnRationalNumberDecode2() throws CborException { List items = new CborBuilder().addTag(30).addArray().add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -106,10 +98,7 @@ public void shouldThrowOnRationalNumberDecode2() throws CborException { @Test(expected = CborException.class) public void shouldThrowOnRationalNumberDecode3() throws CborException { List items = new CborBuilder().addTag(30).addArray().add(true).add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -117,10 +106,7 @@ public void shouldThrowOnRationalNumberDecode3() throws CborException { @Test(expected = CborException.class) public void shouldThrowOnRationalNumberDecode4() throws CborException { List items = new CborBuilder().addTag(30).addArray().add(1).add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -128,10 +114,7 @@ public void shouldThrowOnRationalNumberDecode4() throws CborException { @Test public void shouldDecodeRationalNumber() throws CborException { List items = new CborBuilder().addTag(30).addArray().add(1).add(2).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); assertEquals(new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)), decoder.decodeNext()); } @@ -152,10 +135,7 @@ public void shouldDecodeTaggedTags() throws CborException { @Test public void shouldDecodeTaggedRationalNumber() throws CborException { List items = new CborBuilder().addTag(1).addTag(30).addArray().add(1).add(2).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); RationalNumber expected = new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)); @@ -165,15 +145,7 @@ public void shouldDecodeTaggedRationalNumber() throws CborException { @Test public void shouldThrowOnItemWithForgedLength() throws CborException { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - AbstractEncoder maliciousEncoder = new AbstractEncoder(null, buffer) { - @Override - public void encode(Long length) throws CborException { - encodeTypeAndLength(MajorType.UNICODE_STRING, length.longValue()); - } - }; - maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE + 1L)); - byte[] maliciousString = buffer.toByteArray(); + byte[] maliciousString = new byte[] { 0x7a, (byte) 0x80, 0x00, 0x00, 0x00 }; try { CborDecoder.decode(maliciousString); fail("Should have failed the huge allocation"); @@ -181,9 +153,7 @@ public void encode(Long length) throws CborException { assertThat("Exception message", e.getMessage(), containsString("limited to INTMAX")); } - buffer.reset(); - maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE - 1)); - maliciousString = buffer.toByteArray(); + maliciousString = new byte[] { 0x7a, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xfe }; try { CborDecoder.decode(maliciousString); fail("Should have failed the huge allocation"); diff --git a/src/test/java/co/nstant/in/cbor/decoder/LanguageTaggedStringDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/LanguageTaggedStringDecoderTest.java index 20d871bc..ee75ff2e 100644 --- a/src/test/java/co/nstant/in/cbor/decoder/LanguageTaggedStringDecoderTest.java +++ b/src/test/java/co/nstant/in/cbor/decoder/LanguageTaggedStringDecoderTest.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; @@ -22,10 +21,7 @@ public class LanguageTaggedStringDecoderTest { @Test(expected = CborException.class) public void shouldThrowException() throws CborException { List items = new CborBuilder().addTag(38).build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -33,10 +29,7 @@ public void shouldThrowException() throws CborException { @Test(expected = CborException.class) public void testExceptionOnNotAnArray() throws CborException { List items = new CborBuilder().addTag(38).add(true).build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -44,10 +37,7 @@ public void testExceptionOnNotAnArray() throws CborException { @Test(expected = CborException.class) public void testExceptionOnNot2ElementArray() throws CborException { List items = new CborBuilder().addTag(38).addArray().add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -55,10 +45,7 @@ public void testExceptionOnNot2ElementArray() throws CborException { @Test(expected = CborException.class) public void testExceptionOnNotFirstElementIsString() throws CborException { List items = new CborBuilder().addTag(38).addArray().add(true).add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -66,10 +53,7 @@ public void testExceptionOnNotFirstElementIsString() throws CborException { @Test(expected = CborException.class) public void testExceptionOnNotSecondElementIsString() throws CborException { List items = new CborBuilder().addTag(38).addArray().add("en").add(true).end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); decoder.decode(); } @@ -77,10 +61,7 @@ public void testExceptionOnNotSecondElementIsString() throws CborException { @Test public void testDecoding() throws CborException { List items = new CborBuilder().addTag(38).addArray().add("en").add("string").end().build(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(items); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); CborDecoder decoder = new CborDecoder(bais); DataItem item = decoder.decodeNext(); assertEquals(new LanguageTaggedString(new UnicodeString("en"), new UnicodeString("string")), item); diff --git a/src/test/java/co/nstant/in/cbor/decoder/UnicodeStringDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/UnicodeStringDecoderTest.java index ba5a5e68..04522695 100644 --- a/src/test/java/co/nstant/in/cbor/decoder/UnicodeStringDecoderTest.java +++ b/src/test/java/co/nstant/in/cbor/decoder/UnicodeStringDecoderTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertNotNull; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; @@ -19,10 +18,8 @@ public class UnicodeStringDecoderTest { @Test public void shouldDecodeChunkedUnicodeString() throws CborException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(new CborBuilder().startString().add("foo").add("bar").end().build()); - byte[] encodedBytes = baos.toByteArray(); + byte[] encodedBytes = CborEncoder.encodeToBytes( + new CborBuilder().startString().add("foo").add("bar").end().build()); ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(bais); List dataItems = decoder.decode(); diff --git a/src/test/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoderTest.java index 058b9a59..67b6c67c 100644 --- a/src/test/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoderTest.java +++ b/src/test/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoderTest.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.util.List; @@ -25,10 +24,7 @@ public void shouldDecodeBigNumbers() throws CborException { BigInteger value = BigInteger.ONE; for (int i = 1; i < 64; i++) { value = value.shiftLeft(1); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(baos); - encoder.encode(new CborBuilder().add(value).build()); - byte[] encodedBytes = baos.toByteArray(); + byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(value).build()); ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); CborDecoder decoder = new CborDecoder(bais); List dataItems = decoder.decode(); diff --git a/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java deleted file mode 100644 index 7d738831..00000000 --- a/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package co.nstant.in.cbor.encoder; - -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -import org.junit.Test; - -import co.nstant.in.cbor.CborBuilder; -import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; -import co.nstant.in.cbor.model.MajorType; - -public class AbstractEncoderTest { - - private class TestEncoder extends AbstractEncoder { - - public TestEncoder(OutputStream outputStream) { - super(null, outputStream); - } - - @Override - public void encode(Object dataItem) throws CborException { - } - - public void doEncodeTypeAndLength(long length) throws CborException { - encodeTypeAndLength(MajorType.ARRAY, length); - } - - } - - @Test(expected = CborException.class) - public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException { - final int[] counter = new int[1]; - new CborEncoder(new OutputStream() { - - @Override - public synchronized void write(int b) throws IOException { - if (++counter[0] == 3) { - throw new IOException(); - } - } - - }).encode(new CborBuilder().startString().add("string").end().build()); - } - - @Test(expected = CborException.class) - public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException { - new CborEncoder(new OutputStream() { - - @Override - public synchronized void write(int b) throws IOException { - throw new IOException(); - } - - }).encode(new CborBuilder().startArray().add(1).end().build()); - } - - @Test - public void shallEncode32bit() throws CborException { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - TestEncoder encoder = new TestEncoder(outputStream); - encoder.doEncodeTypeAndLength(4294967296L); - byte[] bytes = outputStream.toByteArray(); - assertEquals(9, bytes.length); - assertEquals((byte) 0x9B, bytes[0]); - assertEquals((byte) 0x00, bytes[1]); - assertEquals((byte) 0x00, bytes[2]); - assertEquals((byte) 0x00, bytes[3]); - assertEquals((byte) 0x01, bytes[4]); - assertEquals((byte) 0x00, bytes[5]); - assertEquals((byte) 0x00, bytes[6]); - assertEquals((byte) 0x00, bytes[7]); - assertEquals((byte) 0x00, bytes[8]); - } - -} diff --git a/src/test/java/co/nstant/in/cbor/encoder/ByteStringEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/ByteStringEncoderTest.java index adf7ccf9..15df54e8 100644 --- a/src/test/java/co/nstant/in/cbor/encoder/ByteStringEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/encoder/ByteStringEncoderTest.java @@ -1,31 +1,27 @@ package co.nstant.in.cbor.encoder; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; public class ByteStringEncoderTest { @Test - public void shouldEncodeNullString() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + public void shouldEncodeNullString() { List dataItems = new CborBuilder().add((ByteString) null).build(); - new CborEncoder(byteArrayOutputStream).encode(dataItems); + CborEncoder.encodeToBytes(dataItems); } @Test - public void shouldEncodeChunkedString() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + public void shouldEncodeChunkedString() { List dataItems = new CborBuilder().add(new ByteString(null)).add(new ByteString("test".getBytes())) .startByteString("test".getBytes()).end().build(); - new CborEncoder(byteArrayOutputStream).encode(dataItems); + CborEncoder.encodeToBytes(dataItems); } } diff --git a/src/test/java/co/nstant/in/cbor/encoder/MapEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/MapEncoderTest.java index 6f9ffce9..64d78864 100644 --- a/src/test/java/co/nstant/in/cbor/encoder/MapEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/encoder/MapEncoderTest.java @@ -1,23 +1,20 @@ package co.nstant.in.cbor.encoder; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; public class MapEncoderTest { @Test - public void shouldEncodeMap() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + public void shouldEncodeMap() { List dataItems = new CborBuilder().addMap().put(1, true).put(".", true).put(3, true).put("..", true) .put(2, true).put("...", true).end().build(); - new CborEncoder(byteArrayOutputStream).encode(dataItems); + CborEncoder.encodeToBytes(dataItems); } } diff --git a/src/test/java/co/nstant/in/cbor/encoder/RationalNumberEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/RationalNumberEncoderTest.java index bf3cde5b..241bc7ef 100644 --- a/src/test/java/co/nstant/in/cbor/encoder/RationalNumberEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/encoder/RationalNumberEncoderTest.java @@ -4,13 +4,10 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import org.junit.Before; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.Array; import co.nstant.in.cbor.model.RationalNumber; @@ -22,19 +19,10 @@ public class RationalNumberEncoderTest { private static final UnsignedInteger ONE = new UnsignedInteger(1); private static final UnsignedInteger TWO = new UnsignedInteger(2); - private ByteArrayOutputStream outputStream; - private CborEncoder encoder; - - @Before - public void setup() { - outputStream = new ByteArrayOutputStream(); - encoder = new CborEncoder(outputStream); - } - @Test public void shouldEncode() throws CborException { - encoder.encode(new RationalNumber(ONE, TWO)); - ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + byte[] bytes = new RationalNumber(ONE, TWO).encodeToBytes(); + ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); CborDecoder decoder = new CborDecoder(inputStream); Array expected = new Array(); diff --git a/src/test/java/co/nstant/in/cbor/encoder/SpecialEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/SpecialEncoderTest.java index 17898904..4ae7aa1b 100644 --- a/src/test/java/co/nstant/in/cbor/encoder/SpecialEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/encoder/SpecialEncoderTest.java @@ -1,9 +1,7 @@ package co.nstant.in.cbor.encoder; -import org.junit.Before; import org.junit.Test; -import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.Special; import co.nstant.in.cbor.model.SpecialType; @@ -17,36 +15,24 @@ public Mock(SpecialType specialType) { } - private SpecialEncoder encoder; - - @Before - public void setup() { - encoder = new SpecialEncoder(null, null); - } - - @Test(expected = CborException.class) - public void shouldNotEncodeReserved() throws CborException { - encoder.encode(new Mock(SpecialType.UNALLOCATED)); - } - - @Test(expected = CborException.class) - public void shouldVerifyImplementation1() throws CborException { - encoder.encode(new Mock(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT)); + @Test(expected = ClassCastException.class) + public void shouldExpectDoublePrecisionFloatImplementation() { + new Mock(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT).encodeToBytes(); } - @Test(expected = CborException.class) - public void shouldVerifyImplementation2() throws CborException { - encoder.encode(new Mock(SpecialType.IEEE_754_HALF_PRECISION_FLOAT)); + @Test(expected = ClassCastException.class) + public void shouldExpectHalfPrecisionFloatImplementation() { + new Mock(SpecialType.IEEE_754_HALF_PRECISION_FLOAT).encodeToBytes(); } - @Test(expected = CborException.class) - public void shouldVerifyImplementation3() throws CborException { - encoder.encode(new Mock(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT)); + @Test(expected = ClassCastException.class) + public void shouldExpectSinglePrecisionFloatImplementation() { + new Mock(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT).encodeToBytes(); } - @Test(expected = CborException.class) - public void shouldVerifyImplementation4() throws CborException { - encoder.encode(new Mock(SpecialType.SIMPLE_VALUE_NEXT_BYTE)); + @Test(expected = ClassCastException.class) + public void shouldExpectSimpleValueImplementation() { + new Mock(SpecialType.SIMPLE_VALUE_NEXT_BYTE).encodeToBytes(); } } diff --git a/src/test/java/co/nstant/in/cbor/encoder/UnicodeStringEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/UnicodeStringEncoderTest.java index afc103e2..65942d95 100644 --- a/src/test/java/co/nstant/in/cbor/encoder/UnicodeStringEncoderTest.java +++ b/src/test/java/co/nstant/in/cbor/encoder/UnicodeStringEncoderTest.java @@ -1,29 +1,25 @@ package co.nstant.in.cbor.encoder; -import java.io.ByteArrayOutputStream; import java.util.List; import org.junit.Test; import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborEncoder; -import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; public class UnicodeStringEncoderTest { @Test - public void shouldEncodeNullString() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + public void shouldEncodeNullString() { List dataItems = new CborBuilder().add((String) null).build(); - new CborEncoder(byteArrayOutputStream).encode(dataItems); + CborEncoder.encodeToBytes(dataItems); } @Test - public void shouldEncodeChunkedString() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + public void shouldEncodeChunkedString() { List dataItems = new CborBuilder().startString("test").end().build(); - new CborEncoder(byteArrayOutputStream).encode(dataItems); + CborEncoder.encodeToBytes(dataItems); } } diff --git a/src/test/java/co/nstant/in/cbor/examples/Example12Test.java b/src/test/java/co/nstant/in/cbor/examples/Example12Test.java index 2b2f9991..31455f28 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example12Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example12Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.math.BigInteger; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; @@ -21,12 +19,9 @@ public class Example12Test { @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteArrayOutputStream); - encoder.encode(new UnsignedInteger(new BigInteger("18446744073709551616"))); + public void shouldEncode() { Assert.assertArrayEquals(new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - byteArrayOutputStream.toByteArray()); + new UnsignedInteger(new BigInteger("18446744073709551616")).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example14Test.java b/src/test/java/co/nstant/in/cbor/examples/Example14Test.java index e5457f42..ea10603a 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example14Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example14Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.math.BigInteger; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; @@ -21,12 +19,9 @@ public class Example14Test { @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteArrayOutputStream); - encoder.encode(new NegativeInteger(new BigInteger("-18446744073709551617"))); + public void shouldEncode() { Assert.assertArrayEquals(new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - byteArrayOutputStream.toByteArray()); + new NegativeInteger(new BigInteger("-18446744073709551617")).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example41Test.java b/src/test/java/co/nstant/in/cbor/examples/Example41Test.java index f74daaaf..1fc9dbe6 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example41Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example41Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -21,11 +19,8 @@ public class Example41Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf4 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(SimpleValue.FALSE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.FALSE.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example42Test.java b/src/test/java/co/nstant/in/cbor/examples/Example42Test.java index 4f2d99f5..9e1cd279 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example42Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example42Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -21,11 +19,8 @@ public class Example42Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf5 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(SimpleValue.TRUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.TRUE.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example43Test.java b/src/test/java/co/nstant/in/cbor/examples/Example43Test.java index e59e5c35..a9b8d219 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example43Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example43Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -21,11 +19,8 @@ public class Example43Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf6 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(SimpleValue.NULL); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.NULL.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example44Test.java b/src/test/java/co/nstant/in/cbor/examples/Example44Test.java index f5a23ab4..b28a28e5 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example44Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example44Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -21,11 +19,8 @@ public class Example44Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf7 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(SimpleValue.UNDEFINED); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.UNDEFINED.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example45Test.java b/src/test/java/co/nstant/in/cbor/examples/Example45Test.java index be5d75be..bf97b4d7 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example45Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example45Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -22,11 +20,8 @@ public class Example45Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf0 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example46Test.java b/src/test/java/co/nstant/in/cbor/examples/Example46Test.java index 146c3f26..a0ae0421 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example46Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example46Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -22,11 +20,8 @@ public class Example46Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf8, 0x18 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example47Test.java b/src/test/java/co/nstant/in/cbor/examples/Example47Test.java index 3e4df29c..eca29ced 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example47Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example47Test.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SimpleValue; @@ -22,11 +20,8 @@ public class Example47Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf8, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example48Test.java b/src/test/java/co/nstant/in/cbor/examples/Example48Test.java index 81a495af..04b86818 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example48Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example48Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -33,11 +32,8 @@ public Example48Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example49Test.java b/src/test/java/co/nstant/in/cbor/examples/Example49Test.java index e9e085e8..1c779b05 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example49Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example49Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -32,11 +31,8 @@ public Example49Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example50Test.java b/src/test/java/co/nstant/in/cbor/examples/Example50Test.java index 0162f695..899e01b2 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example50Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example50Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -33,11 +32,8 @@ public Example50Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example51Test.java b/src/test/java/co/nstant/in/cbor/examples/Example51Test.java index 6245977e..c5fb09e8 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example51Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example51Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -32,11 +31,8 @@ public Example51Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example52Test.java b/src/test/java/co/nstant/in/cbor/examples/Example52Test.java index 4859874f..4ad68fbf 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example52Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example52Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -32,11 +31,8 @@ public Example52Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example53Test.java b/src/test/java/co/nstant/in/cbor/examples/Example53Test.java index 6c6340db..fc3e73ce 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example53Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example53Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -34,11 +33,8 @@ public Example53Test() { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example63Test.java b/src/test/java/co/nstant/in/cbor/examples/Example63Test.java index 24b93309..8b7896e0 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example63Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example63Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; @@ -22,11 +21,9 @@ public class Example63Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x80 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new CborBuilder().addArray().end().build()); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes( + new CborBuilder().addArray().end().build())); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example64Test.java b/src/test/java/co/nstant/in/cbor/examples/Example64Test.java index 40451205..8a8e4b4e 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example64Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example64Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; @@ -23,11 +22,9 @@ public class Example64Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x83, 0x01, 0x02, 0x03 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new CborBuilder().addArray().add(1).add(2).add(3).end().build()); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes( + new CborBuilder().addArray().add(1).add(2).add(3).end().build())); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example65Test.java b/src/test/java/co/nstant/in/cbor/examples/Example65Test.java index f8a1426f..9bc9d451 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example65Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example65Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -28,11 +27,8 @@ public class Example65Test { 0x04, 0x05 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example66Test.java b/src/test/java/co/nstant/in/cbor/examples/Example66Test.java index ea550d70..de065b42 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example66Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example66Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -29,11 +28,8 @@ public class Example66Test { 0x18, 0x18, 0x19 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example67Test.java b/src/test/java/co/nstant/in/cbor/examples/Example67Test.java index 39ae272a..49dc4b0a 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example67Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example67Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -24,11 +23,8 @@ public class Example67Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa0 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example68Test.java b/src/test/java/co/nstant/in/cbor/examples/Example68Test.java index 4747e5d6..57e1cda1 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example68Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example68Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -24,11 +23,8 @@ public class Example68Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa2, 0x01, 0x02, 0x03, 0x04 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example69Test.java b/src/test/java/co/nstant/in/cbor/examples/Example69Test.java index 6cac7d39..3c59a1fb 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example69Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example69Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example69Test { 0x02, 0x03 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example70Test.java b/src/test/java/co/nstant/in/cbor/examples/Example70Test.java index b106a2a3..118e3288 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example70Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example70Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example70Test { 0x63 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example71Test.java b/src/test/java/co/nstant/in/cbor/examples/Example71Test.java index 87f7dfde..72ba754c 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example71Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example71Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -27,11 +26,8 @@ public class Example71Test { 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example72Test.java b/src/test/java/co/nstant/in/cbor/examples/Example72Test.java index 0baa6412..8db2647b 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example72Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example72Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example72Test { (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example73Test.java b/src/test/java/co/nstant/in/cbor/examples/Example73Test.java index 7a4f8539..7598983d 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example73Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example73Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -25,11 +24,8 @@ public class Example73Test { 0x6e, 0x67, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example74Test.java b/src/test/java/co/nstant/in/cbor/examples/Example74Test.java index f9a9b78d..98d82ab5 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example74Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example74Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -27,11 +26,8 @@ public class Example74Test { private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x9f, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example75Test.java b/src/test/java/co/nstant/in/cbor/examples/Example75Test.java index f725e39a..826881cd 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example75Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example75Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -29,11 +28,8 @@ public class Example75Test { 0x04, 0x05, (byte) 0xff, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example76Test.java b/src/test/java/co/nstant/in/cbor/examples/Example76Test.java index d86dbcc0..4e32fcd9 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example76Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example76Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -28,11 +27,8 @@ public class Example76Test { 0x04, 0x05, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example77Test.java b/src/test/java/co/nstant/in/cbor/examples/Example77Test.java index c459052b..11d3657e 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example77Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example77Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example77Test { 0x04, 0x05, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example78Test.java b/src/test/java/co/nstant/in/cbor/examples/Example78Test.java index b66852a7..2434cc19 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example78Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example78Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example78Test { (byte) 0x82, 0x04, 0x05 }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example79Test.java b/src/test/java/co/nstant/in/cbor/examples/Example79Test.java index 2ae6fe25..87eefd27 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example79Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example79Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -30,11 +29,8 @@ public class Example79Test { 0x18, 0x19, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example80Test.java b/src/test/java/co/nstant/in/cbor/examples/Example80Test.java index c3b72ad3..ba049614 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example80Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example80Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example80Test { 0x02, 0x03, (byte) 0xff, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example81Test.java b/src/test/java/co/nstant/in/cbor/examples/Example81Test.java index ce816c75..fe6eb61f 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example81Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example81Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example81Test { 0x63, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example82Test.java b/src/test/java/co/nstant/in/cbor/examples/Example82Test.java index 9b2f1190..4f335011 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example82Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example82Test.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.examples; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -26,11 +25,8 @@ public class Example82Test { 0x41, 0x6d, 0x74, 0x21, (byte) 0xff }; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/examples/Example83Test.java b/src/test/java/co/nstant/in/cbor/examples/Example83Test.java index aabc4530..2557ddb3 100644 --- a/src/test/java/co/nstant/in/cbor/examples/Example83Test.java +++ b/src/test/java/co/nstant/in/cbor/examples/Example83Test.java @@ -9,7 +9,6 @@ import org.junit.Test; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -38,11 +37,8 @@ public class Example83Test { (byte) 0xF7, 0x61, 0x63}; @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(VALUE); - Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractByteStringTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractByteStringTest.java index 5cf1044e..ead52774 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractByteStringTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractByteStringTest.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.ByteString; import co.nstant.in.cbor.model.DataItem; @@ -24,11 +22,8 @@ public AbstractByteStringTest(byte[] value, byte[] encodedValue) { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new ByteString(value)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(encodedValue, new ByteString(value).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractDataItemTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractDataItemTest.java index 3bd2e88e..4df8b53b 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractDataItemTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractDataItemTest.java @@ -1,21 +1,15 @@ package co.nstant.in.cbor.model; -import java.io.ByteArrayOutputStream; - import org.junit.Assert; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; public abstract class AbstractDataItemTest { protected void shouldEncodeAndDecode(String message, DataItem dataItem) throws CborException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteArrayOutputStream); - encoder.encode(dataItem); - byte[] bytes = byteArrayOutputStream.toByteArray(); + byte[] bytes = dataItem.encodeToBytes(); DataItem object = CborDecoder.decode(bytes).get(0); Assert.assertEquals(message, dataItem, object); } diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractDoublePrecisionFloatTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractDoublePrecisionFloatTest.java index b8515395..961c31b5 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractDoublePrecisionFloatTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractDoublePrecisionFloatTest.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; @@ -10,7 +9,6 @@ import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.DoublePrecisionFloat; @@ -26,12 +24,9 @@ public AbstractDoublePrecisionFloatTest(double value, byte[] encodedValue) { } @Test - public void shouldEncode() throws CborException { + public void shouldEncode() { List dataItems = new CborBuilder().add(value).build(); - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(dataItems.get(0)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + Assert.assertArrayEquals(encodedValue, dataItems.get(0).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractHalfPrecisionFloatTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractHalfPrecisionFloatTest.java index e7fd1a2c..eb363ca1 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractHalfPrecisionFloatTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractHalfPrecisionFloatTest.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.HalfPrecisionFloat; @@ -25,10 +23,7 @@ public AbstractHalfPrecisionFloatTest(float value, byte[] encodedValue) { @Test public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new HalfPrecisionFloat(value)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + Assert.assertArrayEquals(encodedValue, new HalfPrecisionFloat(value).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractNumberTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractNumberTest.java index a277497d..d96fa2e5 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractNumberTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractNumberTest.java @@ -1,7 +1,6 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.math.BigInteger; import java.util.List; @@ -11,7 +10,6 @@ import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.Number; @@ -32,12 +30,9 @@ public AbstractNumberTest(BigInteger value, byte[] encodedValue) { } @Test - public void shouldEncode() throws CborException { + public void shouldEncode() { List dataItems = new CborBuilder().add(value).build(); - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(dataItems.get(0)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + Assert.assertArrayEquals(encodedValue, dataItems.get(0).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractSinglePrecisionFloatTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractSinglePrecisionFloatTest.java index 30b6ec35..c6463cc2 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractSinglePrecisionFloatTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractSinglePrecisionFloatTest.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.SinglePrecisionFloat; @@ -24,11 +22,8 @@ public AbstractSinglePrecisionFloatTest(float value, byte[] encodedValue) { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new SinglePrecisionFloat(value)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(encodedValue, new SinglePrecisionFloat(value).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/AbstractStringTest.java b/src/test/java/co/nstant/in/cbor/model/AbstractStringTest.java index 77b416ce..12d63ee9 100644 --- a/src/test/java/co/nstant/in/cbor/model/AbstractStringTest.java +++ b/src/test/java/co/nstant/in/cbor/model/AbstractStringTest.java @@ -1,14 +1,12 @@ package co.nstant.in.cbor.model; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import org.junit.Assert; import org.junit.Test; import co.nstant.in.cbor.CborDecoder; -import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.model.DataItem; import co.nstant.in.cbor.model.UnicodeString; @@ -24,11 +22,8 @@ public AbstractStringTest(String value, byte[] encodedValue) { } @Test - public void shouldEncode() throws CborException { - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - CborEncoder encoder = new CborEncoder(byteOutputStream); - encoder.encode(new UnicodeString(value)); - Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray()); + public void shouldEncode() { + Assert.assertArrayEquals(encodedValue, new UnicodeString(value).encodeToBytes()); } @Test diff --git a/src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java b/src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java index 0db1264f..0508402a 100644 --- a/src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java +++ b/src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java @@ -10,7 +10,7 @@ public class ChunkableDataItemTest { private class TestDataItem extends ChunkableDataItem { protected TestDataItem() { - super(MajorType.INVALID); + super(MajorType.SPECIAL); } } diff --git a/src/test/java/co/nstant/in/cbor/model/DataItemTest.java b/src/test/java/co/nstant/in/cbor/model/DataItemTest.java index fc7f3447..b5e488d7 100644 --- a/src/test/java/co/nstant/in/cbor/model/DataItemTest.java +++ b/src/test/java/co/nstant/in/cbor/model/DataItemTest.java @@ -13,7 +13,7 @@ public class DataItemTest { private class TestDataItem extends DataItem { protected TestDataItem() { - super(MajorType.INVALID); + super(MajorType.SPECIAL); } } diff --git a/src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java b/src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java index 00190584..1fe35912 100644 --- a/src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java +++ b/src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java @@ -3,51 +3,53 @@ import org.junit.Assert; import org.junit.Test; +import co.nstant.in.cbor.CborException; + public class MajorTypeTest { @Test - public void shouldParseUnsignedInteger() { + public void shouldParseUnsignedInteger() throws CborException { Assert.assertEquals(MajorType.UNSIGNED_INTEGER, MajorType.ofByte(0b000_00000)); } @Test - public void shouldParseNegativeInteger() { + public void shouldParseNegativeInteger() throws CborException { Assert.assertEquals(MajorType.NEGATIVE_INTEGER, MajorType.ofByte(0b001_00000)); } @Test - public void shouldParseByteString() { + public void shouldParseByteString() throws CborException { Assert.assertEquals(MajorType.BYTE_STRING, MajorType.ofByte(0b010_00000)); } @Test - public void shouldParseUnicodeString() { + public void shouldParseUnicodeString() throws CborException { Assert.assertEquals(MajorType.UNICODE_STRING, MajorType.ofByte(0b011_00000)); } @Test - public void shouldParseArray() { + public void shouldParseArray() throws CborException { Assert.assertEquals(MajorType.ARRAY, MajorType.ofByte(0b100_00000)); } @Test - public void shouldParseMap() { + public void shouldParseMap() throws CborException { Assert.assertEquals(MajorType.MAP, MajorType.ofByte(0b101_00000)); } @Test - public void shouldParseTag() { + public void shouldParseTag() throws CborException { Assert.assertEquals(MajorType.TAG, MajorType.ofByte(0b110_00000)); } @Test - public void shouldParseSpecial() { + public void shouldParseSpecial() throws CborException { Assert.assertEquals(MajorType.SPECIAL, MajorType.ofByte(0b111_00000)); } - @Test - public void shouldReturnInvalidOnInvalidByteValue() { - Assert.assertEquals(MajorType.INVALID, MajorType.ofByte(0xffffffff)); + @Test(expected = CborException.class) + public void shouldReturnThrowOnInvalidByteValue() throws CborException { + MajorType.ofByte(0xffffffff); } } diff --git a/src/test/java/co/nstant/in/cbor/model/SpecialTypeTest.java b/src/test/java/co/nstant/in/cbor/model/SpecialTypeTest.java index 1d530c7d..5ef08c91 100644 --- a/src/test/java/co/nstant/in/cbor/model/SpecialTypeTest.java +++ b/src/test/java/co/nstant/in/cbor/model/SpecialTypeTest.java @@ -3,13 +3,23 @@ import org.junit.Assert; import org.junit.Test; +import co.nstant.in.cbor.CborException; + public class SpecialTypeTest { - @Test - public void shouldDetectUnallocated() { - Assert.assertTrue(SpecialType.ofByte(28).equals(SpecialType.UNALLOCATED)); - Assert.assertTrue(SpecialType.ofByte(29).equals(SpecialType.UNALLOCATED)); - Assert.assertTrue(SpecialType.ofByte(30).equals(SpecialType.UNALLOCATED)); + @Test(expected = CborException.class) + public void shouldDetectUnallocated28() throws CborException { + SpecialType.ofByte(28); + } + + @Test(expected = CborException.class) + public void shouldDetectUnallocated29() throws CborException { + SpecialType.ofByte(29); + } + + @Test(expected = CborException.class) + public void shouldDetectUnallocated30() throws CborException { + SpecialType.ofByte(30); } }