Skip to content

Commit

Permalink
Merge pull request #175 from AndrewScull/encodeToBytes
Browse files Browse the repository at this point in the history
Encode to bytes
  • Loading branch information
c-rack authored Sep 17, 2023
2 parents 0b251b5 + 05fe06b commit 0a80833
Show file tree
Hide file tree
Showing 81 changed files with 735 additions and 1,220 deletions.
1 change: 0 additions & 1 deletion src/main/java/co/nstant/in/cbor/CborDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public DataItem decodeNext() throws CborException {
return next;
}
}
case INVALID:
default:
throw new CborException("Not implemented major type " + symbol);
}
Expand Down
112 changes: 48 additions & 64 deletions src/main/java/co/nstant/in/cbor/CborEncoder.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -78,52 +57,57 @@ public void encode(List<DataItem> 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<DataItem> 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();
}

}
Loading

0 comments on commit 0a80833

Please sign in to comment.