/** * Jackson doesn't have native support for timestamp. As per the RFC 7049 * (https://tools.ietf.org/html/rfc7049#section-2.4.1) we will need to * write a tag and write the epoch. */ @Override public StructuredJsonGenerator writeValue(Date date, TimestampFormat timestampFormat) { if (!(getGenerator() instanceof CBORGenerator)) { throw new IllegalStateException("SdkCborGenerator is not created with a CBORGenerator."); } CBORGenerator generator = (CBORGenerator) getGenerator(); try { generator.writeTag(CBOR_TAG_TIMESTAP); generator.writeNumber(date.getTime()); } catch (IOException e) { throw new JsonGenerationException(e); } return this; }
@Override public void close() throws IOException { // First: let's see that we still have buffers... if ((_outputBuffer != null) && isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) { while (true) { JsonStreamContext ctxt = getOutputContext(); if (ctxt.inArray()) { writeEndArray(); } else if (ctxt.inObject()) { writeEndObject(); } else { break; } } } // boolean wasClosed = _closed; super.close(); _flushBuffer(); if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) { _out.close(); } else { // If we can't close it, we should at least flush _out.flush(); } // Internal buffer(s) generator has can now be released as well _releaseBuffers(); }
protected final void _writeChunkedString(char[] text, int offset, int len) throws IOException { // need to use a marker first _writeByte(BYTE_STRING_INDEFINITE); while (len > MAX_LONG_STRING_CHARS) { _ensureSpace(MAX_LONG_STRING_BYTES); // marker and single-byte length? int ix = _outputTail; int actual = _encode(_outputTail+3, text, offset, offset+MAX_LONG_STRING_CHARS); final byte[] buf = _outputBuffer; buf[ix++] = BYTE_STRING_2BYTE_LEN; buf[ix++] = (byte) (actual >> 8); buf[ix++] = (byte) actual; _outputTail = ix+actual; offset += MAX_LONG_STRING_CHARS; len -= MAX_LONG_STRING_CHARS; } // and for the last chunk, just use recursion if (len > 0) { _writeString(text, offset, len); } // plus end marker _writeByte(BYTE_BREAK); }
@Override public void writeNumber(long l) throws IOException { if (_cfgMinimalInts) { // First: maybe 32 bits is enough? if (l <= MAX_INT_AS_LONG && l >= MIN_INT_AS_LONG) { writeNumber((int) l); return; } } _verifyValueWrite("write number"); _ensureRoomForOutput(9); if (l < 0L) { l += 1; l = -l; _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_NEG + SUFFIX_UINT64_ELEMENTS); } else { _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_POS + SUFFIX_UINT64_ELEMENTS); } int i = (int) (l >> 32); _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; i = (int) l; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
@Override public void writeNumber(float f) throws IOException { // Ok, now, we needed token type byte plus 5 data bytes (7 bits each) _ensureRoomForOutput(6); _verifyValueWrite("write number"); /* * 17-Apr-2010, tatu: could also use 'floatToIntBits', but it seems more * accurate to use exact representation; and possibly faster. However, * if there are cases where collapsing of NaN was needed (for non-Java * clients), this can be changed */ int i = Float.floatToRawIntBits(f); _outputBuffer[_outputTail++] = BYTE_FLOAT32; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
private final CBORGenerator _createCBORGenerator(IOContext ctxt, int stdFeat, int formatFeat, ObjectCodec codec, OutputStream out) throws IOException { // false -> we won't manage the stream unless explicitly directed to CBORGenerator gen = new CBORGenerator(ctxt, stdFeat, formatFeat, _objectCodec, out); if (CBORGenerator.Feature.WRITE_TYPE_HEADER.enabledIn(formatFeat)) { gen.writeTag(CBORConstants.TAG_ID_SELF_DESCRIBE); } return gen; }
protected final void _writeString(char[] text, int offset, int len) throws IOException _ensureSpace(MAX_SHORT_STRING_BYTES); // can afford approximate length int actual = _encode(_outputTail+1, text, offset, offset+len); final byte[] buf = _outputBuffer; int ix = _outputTail; _ensureSpace(MAX_MEDIUM_STRING_BYTES); // short enough, can approximate int actual = _encode(_outputTail+2, text, offset, offset+len); final byte[] buf = _outputBuffer; int ix = _outputTail; _ensureSpace(MAX_LONG_STRING_BYTES); // calculate accurate length to avoid extra flushing int ix = _outputTail; int actual = _encode(ix+3, text, offset, offset+len); final byte[] buf = _outputBuffer; buf[ix++] = BYTE_STRING_2BYTE_LEN; return; _writeChunkedString(text, offset, len);
_throwIllegalSurrogate(c); _throwIllegalSurrogate(c); c = _convertSurrogate(c, str[i++]); if (c > 0x10FFFF) { // illegal in JSON as well as in XML _throwIllegalSurrogate(c);
protected final void _ensureSpace(int needed) throws IOException { if ((_outputTail + needed + 3) > _outputEnd) { _flushBuffer(); } }
cborG.writeStartObject(); cborG.writeStringField("type", request.getType().name()); cborG.writeStringField("code", request.getCode().name()); cborG.writeNumberField("mid", request.getMID()); cborG.writeNumberField("accept", request.getOptions().getAccept()); cborG.writeEndObject(); cborG.close();
@Override public final void flush() throws IOException { _flushBuffer(); if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) { _out.flush(); } }
protected void maybeCopyTag(JsonParser p) throws IOException { if (p instanceof CBORParser) { if (p.hasCurrentToken()) { final int currentTag = ((CBORParser) p).getCurrentTag(); if (currentTag != -1) { writeTag(currentTag); } } } }
private final void _writeLongValue(long l) throws IOException { _ensureRoomForOutput(9); if (l < 0) { l += 1; l = -l; _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_NEG + SUFFIX_UINT64_ELEMENTS); } else { _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_POS + SUFFIX_UINT64_ELEMENTS); } int i = (int) (l >> 32); _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; i = (int) l; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
private final void _writeNumberNoCheck(long l) throws IOException { if (_cfgMinimalInts) { if (l <= MAX_INT_AS_LONG && l >= MIN_INT_AS_LONG) { _writeNumberNoCheck((int) l); return; } } _ensureRoomForOutput(9); if (l < 0L) { l += 1; l = -l; _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_NEG + SUFFIX_UINT64_ELEMENTS); } else { _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_POS + SUFFIX_UINT64_ELEMENTS); } int i = (int) (l >> 32); _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; i = (int) l; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
private final int _encode(int outputPtr, String str, int len) { final byte[] outBuf = _outputBuffer; final int outputStart = outputPtr; for (int i = 0; i < len; ++i) { int c = str.charAt(i); if (c > 0x7F) { return _encode2(i, outputPtr, str, len, outputStart); } outBuf[outputPtr++] = (byte) c; } return (outputPtr - outputStart); }
&& isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) { while (true) { JsonStreamContext ctxt = getOutputContext(); if (ctxt.inArray()) { writeEndArray(); } else if (ctxt.inObject()) { writeEndObject(); } else { break; _flushBuffer(); if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) { _out.close(); } else { _releaseBuffers();
protected final void _writeChunkedString(char[] text, int offset, int len) throws IOException { // need to use a marker first _writeByte(BYTE_STRING_INDEFINITE); while (len > MAX_LONG_STRING_CHARS) { _ensureSpace(MAX_LONG_STRING_BYTES); // marker and single-byte length? int ix = _outputTail; int actual = _encode(_outputTail+3, text, offset, offset+MAX_LONG_STRING_CHARS); final byte[] buf = _outputBuffer; buf[ix++] = BYTE_STRING_2BYTE_LEN; buf[ix++] = (byte) (actual >> 8); buf[ix++] = (byte) actual; _outputTail = ix+actual; offset += MAX_LONG_STRING_CHARS; len -= MAX_LONG_STRING_CHARS; } // and for the last chunk, just use recursion if (len > 0) { _writeString(text, offset, len); } // plus end marker _writeByte(BYTE_BREAK); }
@Override public void writeNumber(long l) throws IOException { if (_cfgMinimalInts) { // First: maybe 32 bits is enough? if (l <= MAX_INT_AS_LONG && l >= MIN_INT_AS_LONG) { writeNumber((int) l); return; } } _verifyValueWrite("write number"); _ensureRoomForOutput(9); if (l < 0L) { l += 1; l = -l; _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_NEG + 27); } else { _outputBuffer[_outputTail++] = (PREFIX_TYPE_INT_POS + 27); } int i = (int) (l >> 32); _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; i = (int) l; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
@Override public void writeNumber(float f) throws IOException { // Ok, now, we needed token type byte plus 5 data bytes (7 bits each) _ensureRoomForOutput(6); _verifyValueWrite("write number"); /* 17-Apr-2010, tatu: could also use 'floatToIntBits', but it seems more accurate to use * exact representation; and possibly faster. However, if there are cases * where collapsing of NaN was needed (for non-Java clients), this can * be changed */ int i = Float.floatToRawIntBits(f); _outputBuffer[_outputTail++] = BYTE_FLOAT32; _outputBuffer[_outputTail++] = (byte) (i >> 24); _outputBuffer[_outputTail++] = (byte) (i >> 16); _outputBuffer[_outputTail++] = (byte) (i >> 8); _outputBuffer[_outputTail++] = (byte) i; }
private final CBORGenerator _createCBORGenerator(IOContext ctxt, int stdFeat, int formatFeat, ObjectCodec codec, OutputStream out) throws IOException { // false -> we won't manage the stream unless explicitly directed to CBORGenerator gen = new CBORGenerator(ctxt, stdFeat, formatFeat, _objectCodec, out); if (CBORGenerator.Feature.WRITE_TYPE_HEADER.enabledIn(formatFeat)) { gen.writeTag(CBORConstants.TAG_ID_SELF_DESCRIBE); } return gen; }