Refine search
/** * Serialises an object. * * @param object the object to be serialised * @param prettyPrint true if the object should be serialised with pretty printing * @param fieldsToExclude optional property names to exclude from the json * @return the provided object serialised (with pretty printing) into bytes * @throws SerialisationException if the object fails to serialise */ public static byte[] serialise(final Object object, final boolean prettyPrint, final String... fieldsToExclude) throws SerialisationException { final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); try { serialise(object, JSON_FACTORY.createGenerator(byteArrayBuilder, JsonEncoding.UTF8), prettyPrint, fieldsToExclude); } catch (final IOException e) { throw new SerialisationException(e.getMessage(), e); } return byteArrayBuilder.toByteArray(); }
public ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
int bits = decodeBase64Char(ch); if (bits < 0) { _reportInvalidBase64(ch, 0, null); _reportBase64EOF(); if (!usesPadding()) { decodedData >>= 4; builder.append(decodedData); break; builder.append(decodedData); continue; if (!usesPadding()) { decodedData >>= 2; builder.appendTwoBytes(decodedData); break; builder.appendTwoBytes(decodedData); } else { builder.appendThreeBytes(decodedData);
private int _appendByte(int ch, int esc, ByteArrayBuilder bb, int ptr) { bb.setCurrentSegmentLength(ptr); bb.append('\\'); if (esc < 0) { // standard escape bb.append('u'); if (ch > 0xFF) { int hi = (ch >> 8); bb.append(HB[hi >> 4]); bb.append(HB[hi & 0xF]); ch &= 0xFF; } else { bb.append('0'); bb.append('0'); } bb.append(HB[ch >> 4]); bb.append(HB[ch & 0xF]); } else { // 2-char simple escape bb.append((byte) esc); } return bb.getCurrentSegmentLength(); }
/** * Convenience method for decoding contents of a Base64-encoded String, * using this variant's settings. * * @param input * * @since 2.2.3 * * @throws IllegalArgumentException if input is not valid base64 encoded data */ @SuppressWarnings("resource") public byte[] decode(String input) throws IllegalArgumentException { ByteArrayBuilder b = new ByteArrayBuilder(); decode(input, b); return b.toByteArray(); }
@Test public void testCanDeserialiseVersionZeroPointThreePointFourBitmap() throws IOException { //Bitmap of (2,3000,300000) serialised in 0.3.4 Roaring Bitmap base 64 encoded String serialisedBitmap = "{\"roaringBitmap\":{\"value\":\"OTAAAAIAAAAAAAEABAAAAAIAuAvgkw==\"}}"; RoaringBitmap comparisonBitmap = new RoaringBitmap(); comparisonBitmap.add(2); comparisonBitmap.add(3000); comparisonBitmap.add(300000); JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap); parser.setCodec(new ObjectMapper()); Object o = DESERIALISER.deserialize(parser, null); assertEquals(RoaringBitmap.class, o.getClass()); assertEquals(comparisonBitmap, o); ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder); SERIALISER.serialize((RoaringBitmap) o, jsonGenerator, null); jsonGenerator.flush(); byte[] bytes = byteArrayBuilder.toByteArray(); String reSerialisedBitmap = new String(bytes); byteArrayBuilder = new ByteArrayBuilder(); jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder); SERIALISER.serialize(comparisonBitmap, jsonGenerator, null); jsonGenerator.flush(); String serialisedComparisonBitmap = new String(byteArrayBuilder.toByteArray()); assertNotEquals(reSerialisedBitmap, serialisedBitmap); assertEquals(reSerialisedBitmap, serialisedComparisonBitmap); }
/** * Method that can be used to serialize any Java value as * a byte array. Functionally equivalent to calling * {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream} * and getting bytes, but more efficient. * Encoding used will be UTF-8. *<p> * Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it. */ @SuppressWarnings("resource") public byte[] writeValueAsBytes(Object value) throws JsonProcessingException { ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler()); try { _configAndWriteValue(_jsonFactory.createGenerator(bb, JsonEncoding.UTF8), value); } catch (JsonProcessingException e) { // to support [JACKSON-758] throw e; } catch (IOException e) { // shouldn't really happen, but is declared as possibility so: throw JsonMappingException.fromUnexpectedIOE(e); } byte[] result = bb.toByteArray(); bb.release(); return result; }
ch = _inputData.readUnsignedByte(); } while (ch <= INT_SPACE); int bits = b64variant.decodeBase64Char(ch); if (bits < 0) { // reached the end, fair and square? if (ch == INT_QUOTE) { return builder.toByteArray(); bits = b64variant.decodeBase64Char(ch); builder.append(decodedData); if (b64variant.usesPadding()) { _handleBase64MissingPadding(b64variant); return builder.toByteArray(); builder.append(decodedData); continue; builder.appendTwoBytes(decodedData); if (b64variant.usesPadding()) { _handleBase64MissingPadding(b64variant); return builder.toByteArray(); builder.appendTwoBytes(decodedData); continue; builder.appendThreeBytes(decodedData);
/** * Method that can be used to serialize any Java value as * a byte array. Functionally equivalent to calling * {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream} * and getting bytes, but more efficient. * Encoding used will be UTF-8. */ public byte[] writeValueAsBytes(Object value) throws IOException, JsonGenerationException, JsonMappingException { ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler()); _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value); byte[] result = bb.toByteArray(); bb.release(); return result; }
protected ByteBuffer toJsonChunk(Object data, boolean stream){ try { ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); bodyWriter.writeValue(byteArrayBuilder, data); if(stream) { byteArrayBuilder.write(NEWLINE_SEPARATOR); } return ByteBuffer.wrap(byteArrayBuilder.toByteArray()); } catch (java.io.IOException e) { throw new UncheckedIOException(e); } }
@Override @SuppressWarnings("resource") public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException { // First: maybe we some special types? if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) { // Embedded byte array would work nicely... Object ob = _currentObject(); if (ob instanceof byte[]) { return (byte[]) ob; } // fall through to error case } if (_currToken != JsonToken.VALUE_STRING) { throw _constructError("Current token ("+_currToken+") not VALUE_STRING (or VALUE_EMBEDDED_OBJECT with byte[]), cannot access as binary"); } final String str = getText(); if (str == null) { return null; } ByteArrayBuilder builder = _byteBuilder; if (builder == null) { _byteBuilder = builder = new ByteArrayBuilder(100); } else { _byteBuilder.reset(); } _decodeBase64(str, builder, b64variant); return builder.toByteArray(); }
_bytes = byteBuilder = new ByteArrayBuilder(null); byte[] outputBuffer = byteBuilder.resetAndGetFirstSegment(); int outputEnd = outputBuffer.length; outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length; outputPtr = 0; outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length; outputPtr = 0; outputBuffer[outputPtr++] = (byte) (0xe0 | (c >> 12)); if (outputPtr >= outputEnd) { outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length; outputPtr = 0; outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length; outputPtr = 0; outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length; outputPtr = 0; outputBuffer = byteBuilder.finishCurrentSegment(); outputEnd = outputBuffer.length;
/** * Method that will complete "manual" output process, coalesce * content (if necessary) and return results as a contiguous buffer. * * @param lastBlockLength Amount of content in the current segment * buffer. * * @return Coalesced contents */ public byte[] completeAndCoalesce(int lastBlockLength) { _currBlockPtr = lastBlockLength; return toByteArray(); }
bb.write(_inputBuffer, _inputPtr, count); _inputPtr += count; len -= count; return bb.toByteArray();
/** * Method called when starting "manual" output: will clear out * current state and return the first segment buffer to fill */ public byte[] resetAndGetFirstSegment() { reset(); return _currBlock; }
@Override public void write(byte[] b) { write(b, 0, b.length); }
public void append(int i) { if (_currBlockPtr >= _currBlock.length) { _allocMore(); } _currBlock[_currBlockPtr++] = (byte) i; }
int x = copy[4]; i1 += x >> 3; _byteArrayBuilder.appendFourBytes(i1); i1 = ((x & 0x7) << 21) + (copy[5] << 14) + (copy[6] << 7) + copy[7]; _byteArrayBuilder.appendThreeBytes(i1); ptr += needed; bytesToDecode -= 7; int x = input[ptr++]; i1 += x >> 3; _byteArrayBuilder.appendFourBytes(i1); i1 = ((x & 0x7) << 21) + (input[ptr++] << 14) + (input[ptr++] << 7) + input[ptr++]; _byteArrayBuilder.appendThreeBytes(i1); bytesToDecode -= 7; avail -= 8;
@Test public void testCanSerialiseAndDeserialise() throws IOException { final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder); RoaringBitmap testBitmap = new RoaringBitmap(); testBitmap.add(2); testBitmap.add(3000); testBitmap.add(300000); for (int i=400000; i<500000; i+=2) { testBitmap.add(i); } SERIALISER.serialize(testBitmap, jsonGenerator, null); jsonGenerator.flush(); byte[] serialisedBitmap = byteArrayBuilder.toByteArray(); JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap); parser.setCodec(new ObjectMapper()); Object o = DESERIALISER.deserialize(parser, null); assertEquals(RoaringBitmap.class, o.getClass()); assertEquals(testBitmap, o); }