Top Description Inners Fields Constructors Methods
com.fasterxml.jackson.dataformat.cbor

public Class CBORGenerator

extends GeneratorBase
Class Inheritance
Imports
java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.util.Arrays, .HashMap, java.io.*, java.math.BigDecimal, .BigInteger, com.fasterxml.jackson.core.*, com.fasterxml.jackson.core.base.GeneratorBase, com.fasterxml.jackson.core.io.IOContext, com.fasterxml.jackson.core.json.DupDetector, com.fasterxml.jackson.core.util.JacksonFeatureSet

JsonGenerator implementation that writes CBOR encoded content.
Author
Tatu Saloranta

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
CBORGenerator.Feature

Enumeration that defines all togglable features for CBOR generator.

Field Summary

Modifier and TypeField and Description
protected boolean
_bufferRecyclable

Flag that indicates whether the output buffer is recycable (and needs to be returned to recycler once we are done) or not.

protected int
_bytesWritten

Let's keep track of how many bytes have been output, may prove useful when debugging.

protected boolean
protected boolean
protected char[]
_charBuffer

Intermediate buffer in which characters of a String are copied before being encoded.

protected final int
protected int
_currentRemainingElements

Number of elements remaining in the current complex structure (if any), when writing defined-length Arrays, Objects; marker {code INDEFINITE_LENGTH} otherwise.

protected int[]
protected int
protected int
_formatFeatures

Bit flag composed of bits that indicate which CBORGenerator.Features are enabled.

protected final OutputStream
protected byte[]
_outputBuffer

Intermediate buffer in which contents are buffered before being written using _out.

protected final int
_outputEnd

Offset to index after the last valid index in _outputBuffer.

protected int
_outputTail

Pointer to the next available byte in _outputBuffer

protected final StreamWriteConstraints
protected CBORWriteContext
protected HashMap<Object, Integer>
_stringRefs

Table of previously referenced text and binary strings when the STRINGREF feature is used.

protected static final int
BYTE_BUFFER_FOR_OUTPUT

Let's ensure that we have big enough output buffer because of safety margins we need for UTF-8 encoding.

private static final int
INDEFINITE_LENGTH

Special value that is use to keep tracks of arrays and maps opened with infinite length

private static final int
MAX_LONG_STRING_BYTES

This is the worst case length (in bytes) of maximum chunk we ever write.

private static final int
MAX_LONG_STRING_CHARS

Longest char chunk we will output is chosen so that it is guaranteed to fit in an empty buffer even if everything encoded in 3-byte sequences; but also fit two full chunks in case of single-byte (ascii) output.

private static final int
private static final int
private static final int
private static final int
private static final int
MIN_BUFFER_LENGTH

To simplify certain operations, we require output buffer length to allow outputting of contiguous 256 character UTF-8 encoded String value.

private static final int[]
protected static final int
REPLACEMENT_CHAR

The replacement character to use to fix invalid Unicode sequences (mismatched surrogate pair).

Inherited from com.fasterxml.jackson.core.base.GeneratorBase:
_cfgNumbersAsStrings_closed_features_ioContext_objectCodec_writeContextDERIVED_FEATURES_MASKMAX_BIG_DECIMAL_SCALESURR1_FIRSTSURR1_LASTSURR2_FIRSTSURR2_LASTWRITE_BINARYWRITE_BOOLEANWRITE_NULLWRITE_NUMBERWRITE_RAWWRITE_STRING

Constructor Summary

AccessConstructor and Description
public
CBORGenerator(IOContext ioCtxt, int stdFeatures, int formatFeatures, ObjectCodec codec, OutputStream out)

public
CBORGenerator(IOContext ioCtxt, int stdFeatures, int formatFeatures, ObjectCodec codec, OutputStream out, byte[]
Buffer to use for output before flushing to the underlying stream
outputBuffer
,
int
Offset pointing past already buffered content; that is, number of bytes of valid content to output, within buffer.
offset
,
boolean bufferRecyclable)

Alternative constructor that may be used to feed partially initialized content.

Method Summary

Modifier and TypeMethod and Description
private int
_appendReplacementChar(byte[] outBuf, int outputPtr)

private int
_decodeAndWriteSurrogate(int surr1, int surr2, byte[] outBuf, int outputPtr)

private final int
_encode(int outputPtr, char[] str, int i, int end)

Helper method called when the whole character sequence is known to fit in the output buffer regardless of UTF-8 expansion.

private final int
_encode(int outputPtr, String str, int len)

private final int
_encode2(int i, int outputPtr, String str, int len, int outputStart)

private final void
protected final void
_ensureSpace(int needed)

private void
protected final void
private int
_invalidSurrogateEnd(int surr1, int surr2, byte[] outBuf, int outputPtr)

private int
_invalidSurrogateStart(int code, byte[] outBuf, int outputPtr)

protected UnsupportedOperationException
private final void
protected void
_releaseBuffers()

Implements abstract com.fasterxml.jackson.core.base.GeneratorBase._releaseBuffers.

Method called to release any buffers generator may be holding, once generator is being closed.
private final int
_shortUTF8Encode2(char[] str, int i, int end, int outputPtr, int outputStart)

Helper method called when the whole character sequence is known to fit in the output buffer, but not all characters are single-byte (ASCII) characters.

protected final void
_verifyValueWrite(String
Additional message used for generating exception message if value output is NOT legal in current generator output state.
typeMsg
)

Implements abstract com.fasterxml.jackson.core.base.GeneratorBase._verifyValueWrite.

Method called before trying to write a value (scalar or structured), to verify that this is legal in current output state, as well as to output separators if and as necessary.
protected void
private final void
_writeByte(byte b)

private final void
_writeBytes(byte[] data, int offset, int len)

private final int
_writeBytes(InputStream in, int bytesLeft)

private final void
_writeBytesLong(byte[] data, int offset, int len)

protected final void
_writeChunkedString(char[] text, int offset, int len)

private final void
private final void
private final void
private final void
_writeIntFull(int markerBase, int i)

private final void
_writeIntMinimal(int markerBase, int i)

private final void
private final void
_writeLengthMarker(int majorType, int i)

private final void
private final void
protected final void
protected final int
_writeString(char[] text, int offset, int len)

public void
assignCurrentValue(Object
Current value to assign for the current context of this generator
v
)

Overrides com.fasterxml.jackson.core.JsonGenerator.assignCurrentValue.

Helper method, usually equivalent to: getOutputContext().setCurrentValue(v);
public boolean
canWriteBinaryNatively()

Overrides com.fasterxml.jackson.core.JsonGenerator.canWriteBinaryNatively.

Introspection method that may be called to see if the underlying data format supports "native" binary data; that is, an efficient output of binary content without encoding.
public void
close()

Overrides com.fasterxml.jackson.core.base.GeneratorBase.close.

Implements java.io.Closeable.close.

Method called to close this generator, so that no more content can be written.
private final void
public CBORGenerator
configure(CBORGenerator.Feature f, boolean state)

public Object
currentValue()

Overrides com.fasterxml.jackson.core.JsonGenerator.currentValue.

Helper method, usually equivalent to: getOutputContext().getCurrentValue();

Note that "current value" is NOT populated (or used) by Streaming parser or generators; it is only used by higher-level data-binding functionality.

public CBORGenerator
public CBORGenerator
public final void
flush()

Implements abstract com.fasterxml.jackson.core.base.GeneratorBase.flush.

Implements java.io.Flushable.flush.

Method called to flush any buffered content to the underlying target (output stream, writer), and to flush the target itself as well.
public Object
getCurrentValue()

Overrides com.fasterxml.jackson.core.base.GeneratorBase.getCurrentValue.

Alias for currentValue(), to be deprecated in later Jackson 2.x versions (and removed from Jackson 3.0).
public int
getFormatFeatures()

Overrides com.fasterxml.jackson.core.JsonGenerator.getFormatFeatures.

Bulk access method for getting state of all FormatFeatures, format-specific on/off configuration settings.
public int
getOutputBuffered()

Overrides com.fasterxml.jackson.core.JsonGenerator.getOutputBuffered.

Method for verifying amount of content that is buffered by generator but not yet flushed to the underlying target (stream, writer), in units (byte, char) that the generator implementation uses for buffering; or -1 if this information is not available.
public JsonStreamContext
getOutputContext()

Overrides com.fasterxml.jackson.core.base.GeneratorBase.getOutputContext.

Note

type was co-variant until Jackson 2.7; reverted back to base type in 2.8 to allow for overriding by subtypes that use custom context type.

public Object
getOutputTarget()

Overrides com.fasterxml.jackson.core.JsonGenerator.getOutputTarget.

Method that can be used to get access to object that is used as target for generated output; this is usually either OutputStream or Writer, depending on what generator was constructed with.
public JacksonFeatureSet<StreamWriteCapability>
getWriteCapabilities()

Overrides com.fasterxml.jackson.core.JsonGenerator.getWriteCapabilities.

Accessor for getting metadata on capabilities of this generator, based on underlying data format being read (directly or indirectly).
public final boolean
public JsonGenerator
overrideFormatFeatures(int
Bit mask of set/clear state for features to change
values
,
int
Bit mask of features to change
mask
)

Overrides com.fasterxml.jackson.core.JsonGenerator.overrideFormatFeatures.

Bulk set method for (re)setting states of FormatFeatures, by specifying values (set / clear) along with a mask, to determine which features to change, if any.
public JsonGenerator
overrideStdFeatures(int
Bit mask of set/clear state for features to change
values
,
int
Bit mask of features to change
mask
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.overrideStdFeatures.

Bulk set method for (re)setting states of features specified by mask.
public void
setCurrentValue(Object
Current value to assign for the current context of this generator
v
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.setCurrentValue.

Alias for assignCurrentValue, to be deprecated in later Jackson 2.x versions (and removed from Jackson 3.0).
public JsonGenerator
setPrettyPrinter(PrettyPrinter
PrettyPrinter to assign, if any; null if none
pp
)

Overrides com.fasterxml.jackson.core.JsonGenerator.setPrettyPrinter.

No way (or need) to indent anything, so let's block any attempts.
public StreamWriteConstraints
streamWriteConstraints()

Overrides com.fasterxml.jackson.core.JsonGenerator.streamWriteConstraints.

Get the constraints to apply when performing streaming writes.
public JsonGenerator
useDefaultPrettyPrinter()

Overrides com.fasterxml.jackson.core.base.GeneratorBase.useDefaultPrettyPrinter.

No way (or need) to indent anything, so let's block any attempts.
public Version
version()

Overrides com.fasterxml.jackson.core.base.GeneratorBase.version.

Implements com.fasterxml.jackson.core.Versioned.version.

Implemented with standard version number detection algorithm, typically using a simple generated class, with information extracted from Maven project file during build.
public void
writeArray(int[]
Array that contains values to write
array
,
int
Offset of the first element to write, within array
offset
,
int
Number of elements in array to write, from `offset` to `offset + len - 1`
length
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeArray.

Value write method that can be called to write a single array (sequence of JsonToken#START_ARRAY, zero or more JsonToken#VALUE_NUMBER_INT, JsonToken#END_ARRAY)
public void
writeArray(long[]
Array that contains values to write
array
,
int
Offset of the first element to write, within array
offset
,
int
Number of elements in array to write, from `offset` to `offset + len - 1`
length
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeArray.

Value write method that can be called to write a single array (sequence of JsonToken#START_ARRAY, zero or more JsonToken#VALUE_NUMBER_INT, JsonToken#END_ARRAY)
public void
writeArray(double[]
Array that contains values to write
array
,
int
Offset of the first element to write, within array
offset
,
int
Number of elements in array to write, from `offset` to `offset + len - 1`
length
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeArray.

Value write method that can be called to write a single array (sequence of JsonToken#START_ARRAY, zero or more JsonToken#VALUE_NUMBER_FLOAT, JsonToken#END_ARRAY)
public void
writeBinary(Base64Variant
Base64 variant to use: defines details such as whether padding is used (and if so, using which character); what is the maximum line length before adding linefeed, and also the underlying alphabet to use.
b64variant
,
byte[]
Buffer that contains binary data to write
data
,
int
Offset in data of the first byte of data to write
offset
,
int
Length of data to write
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeBinary.

Method that will output given chunk of binary data as base64 encoded, as a complete String value (surrounded by double quotes).
public int
writeBinary(InputStream
InputStream to use for reading binary data to write. Will not be closed after successful write operation
data
,
int
(optional) number of bytes that will be available; or -1 to be indicate it is not known. Note that implementations need not support cases where length is not known in advance; this depends on underlying data format: JSON output does NOT require length, other formats may
dataLength
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeBinary.

Similar to writeBinary(Base64Variant, InputStream, int), but assumes default to using the Jackson default Base64 variant (which is Base64Variants#MIME_NO_LINEFEEDS).
public int
writeBinary(Base64Variant
Base64 variant to use
b64variant
,
InputStream
InputStream to use for reading binary data to write. Will not be closed after successful write operation
data
,
int
(optional) number of bytes that will be available; or -1 to be indicate it is not known. If a positive length is given, data MUST provide at least that many bytes: if not, an exception will be thrown. Note that implementations need not support cases where length is not known in advance; this depends on underlying data format: JSON output does NOT require length, other formats may.
dataLength
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.writeBinary.

Method similar to writeBinary(Base64Variant, byte[], int, int), but where input is provided through a stream, allowing for incremental writes without holding the whole input in memory.
public void
writeBoolean(boolean
Boolean value to write
state
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeBoolean.

Method for outputting literal JSON boolean value (one of Strings 'true' and 'false').
public void
writeBytes(byte[] data, int offset, int len)

Method for directly inserting specified bytes in output at current position.

public final void
writeEndArray()

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeEndArray.

Method for writing closing marker of a JSON Array value (character ']'; plus possible white space decoration if pretty-printing is enabled).
public final void
writeEndObject()

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeEndObject.

Method for writing closing marker of an Object value (character '}'; plus possible white space decoration if pretty-printing is enabled).
public final void
writeFieldId(long
Field id to write
id
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeFieldId.

Alternative to writeFieldName(String) that may be used in cases where property key is of numeric type; either where underlying format supports such notion (some binary formats do, unlike JSON), or for convenient conversion into String presentation.
public final void
writeFieldName(String
Field name to write
name
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeFieldName.

Method for writing a field name (JSON String surrounded by double quotes: syntactically identical to a JSON String value), possibly decorated by white space if pretty-printing is enabled.
public final void
writeFieldName(SerializableString
Field name to write
name
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.writeFieldName.

Method similar to writeFieldName(String), main difference being that it may perform better as some of processing (such as quoting of certain characters, or encoding into external encoding if supported by generator) can be done just once and reused for later calls.
public void
writeNull()

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNull.

Method for outputting literal JSON null value.
public void
writeNumber(int
Number value to write
i
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting given value as JSON number.
public void
writeNumber(long
Number value to write
l
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting given value as JSON number.
public void
writeNumber(BigInteger
Number value to write
v
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting given value as JSON number.
public void
writeNumber(double
Number value to write
d
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting indicate JSON numeric value.
public void
writeNumber(float
Number value to write
f
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting indicate JSON numeric value.
public void
writeNumber(BigDecimal
Number value to write
dec
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Method for outputting indicate JSON numeric value.
public void
writeNumber(String
Textual (possibly format) number representation to write
encodedValue
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeNumber.

Write method that can be used for custom numeric types that can not be (easily?) converted to "standard" Java number types.
public void
writeRaw(String
Textual contents to include as-is in output.
text
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeRaw.

Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
public void
writeRaw(String
String that has contents to include as-is in output
text
,
int
Offset within text of the first character to output
offset
,
int
Length of content (from text, starting at offset offset) to output
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeRaw.

Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
public void
writeRaw(char[]
Buffer that has contents to include as-is in output
text
,
int
Offset within text of the first character to output
offset
,
int
Length of content (from text, starting at offset offset) to output
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeRaw.

Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
public void
writeRaw(char
Character to included in output
c
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeRaw.

Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).
public void
writeRaw(byte b)

Method for directly inserting specified byte in output at current position.

public void
writeRawUTF8String(byte[]
Buffer that contains String value to write
raw
,
int
Offset in buffer of the first byte of String value to write
offset
,
int
Length of the String value (in characters) to write
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeRawUTF8String.

Method similar to writeString(String) but that takes as its input a UTF-8 encoded String that is to be output as-is, without additional escaping (type of which depends on data format; backslashes for JSON).
public void
writeRawValue(String
Textual contents to included in output
text
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.writeRawValue.

Method that will force generator to copy input text verbatim without any modifications, but assuming it must constitute a single legal JSON value (number, string, boolean, null, Array or List).
public void
public void
public final void
writeStartArray()

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeStartArray.

Method for writing starting marker of a Array value (for JSON this is character '['; plus possible white space decoration if pretty-printing is enabled).
public void
writeStartArray(Object
"Current value" to assign for the Array context being created
forValue
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeStartArray.

Method for writing start marker of an Array value, similar to writeStartArray(), but also specifying the "current value" to assign to the new Array context being created.
public void
writeStartArray(Object
"Current value" to assign for the Array context being created
forValue
,
int
Number of elements this Array will have: actual number of values written (before matching call to writeEndArray() MUST match; generator MAY verify this is the case (and SHOULD if format itself encodes length)
elementsToWrite
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeStartArray.

Method for writing start marker of an Array value, similar to writeStartArray(), but also specifying the "current value" to assign to the new Array context being created as well as how many elements will be written for the array before calling writeEndArray().
public void
writeStartArray(int
Number of elements this array will have: actual number of values written (before matching call to writeEndArray() MUST match; generator MAY verify this is the case (and SHOULD if format itself encodes length)
elementsToWrite
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeStartArray.

Deprecated
Method for writing start marker of an Array value, similar to writeStartArray(), but also specifying how many elements will be written for the array before calling writeEndArray().
public final void
writeStartObject()

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeStartObject.

Method for writing starting marker of an Object value (character '{'; plus possible white space decoration if pretty-printing is enabled).
public final void
writeStartObject(Object
"Current value" to assign for the Object context being created
forValue
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.writeStartObject.

Method for writing starting marker of an Object value to represent the given Java Object value.
public final void
writeStartObject(int elementsToWrite)

public void
writeStartObject(Object
"Current value" to assign for the Object context being created
forValue
,
int
Number of key/value pairs this Object will have: actual number of entries written (before matching call to writeEndObject() MUST match; generator MAY verify this is the case (and SHOULD if format itself encodes length)
elementsToWrite
)

Overrides com.fasterxml.jackson.core.JsonGenerator.writeStartObject.

Method for writing starting marker of an Object value to represent the given Java Object value.
public void
writeString(String
Text value to write
text
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeString.

Method for outputting a String value.
public final void
writeString(SerializableString
Pre-encoded String value to write
sstr
)

Overrides com.fasterxml.jackson.core.base.GeneratorBase.writeString.

Method similar to writeString(String), but that takes SerializableString which can make this potentially more efficient to call as generator may be able to reuse quoted and/or encoded representation.
public void
writeString(char[]
Buffer that contains String value to write
text
,
int
Offset in buffer of the first character of String value to write
offset
,
int
Length of the String value (in characters) to write
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeString.

Method for outputting a String value.
public void
writeTag(int
Positive integer (0 or higher)
tagId
)

Method for writing out an explicit CBOR Tag.

public final void
writeUTF8String(byte[]
Buffer that contains String value to write
text
,
int
Offset in buffer of the first byte of String value to write
offset
,
int
Length of the String value (in characters) to write
len
)

Implements abstract com.fasterxml.jackson.core.JsonGenerator.writeUTF8String.

Method similar to writeString(String) but that takes as its input a UTF-8 encoded String which has not been escaped using whatever escaping scheme data format requires (for JSON that is backslash-escaping for control characters and double-quotes; for other formats something else).
Inherited from com.fasterxml.jackson.core.base.GeneratorBase:
_asString_checkRangeBoundsForByteArray_checkRangeBoundsForCharArray_checkRangeBoundsForString_checkStdFeatureChanges_constructDefaultPrettyPrinter_decodeSurrogatedisableenablegetCodecgetFeatureMaskisClosedisEnabledsetCodecsetFeatureMaskwriteObjectwriteRawValuewriteTree