/** * 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(); }
/** * 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(); }
/** * 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(); }
/** * Method for accessing textual contents assuming they were * base64 encoded; if so, they are decoded and resulting binary * data is returned. */ @SuppressWarnings("resource") public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { final String str = _value.trim(); ByteArrayBuilder builder = new ByteArrayBuilder(4 + ((str.length() * 3) << 2)); try { b64variant.decode(str, builder); } catch (IllegalArgumentException e) { throw InvalidFormatException.from(null, String.format( "Cannot access contents of TextNode as binary due to broken Base64 encoding: %s", e.getMessage()), str, byte[].class); } return builder.toByteArray(); }
/** * Convenience method for decoding contents of a Base64-encoded String, * using this variant's settings. * * @param input * * @throws IllegalArgumentException if input is not valid base64 encoded data */ public byte[] decode(String input) throws IllegalArgumentException { try (ByteArrayBuilder b = new ByteArrayBuilder()) { decode(input, b); return b.toByteArray(); } }
/** * 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(); }
@SuppressWarnings("resource") @Override // since 2.7 public byte[] getBinaryValue(Base64Variant variant) throws IOException { if (_binaryValue == null) { if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token ("+_currToken+") not VALUE_STRING, can not access as binary"); } ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, variant); _binaryValue = builder.toByteArray(); } return _binaryValue; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token (%s) not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary", _currToken); } if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } return _binaryValue; }
/** * 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; }
/** * 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(_generatorFactory._getBufferRecycler()); try { _configAndWriteValue(_generatorFactory.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; }
@SuppressWarnings("resource") @Override public byte[] getBinaryValue(Base64Variant variant) throws IOException { if (_binaryValue == null) { if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token (%s) not VALUE_EMBEDDED_OBJECT or VALUE_STRING, can not access as binary", _currToken); } ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, variant); _binaryValue = builder.toByteArray(); } return _binaryValue; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token (%s) not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary", _currToken); } if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } return _binaryValue; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if (_currToken != JsonToken.VALUE_STRING && (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT || _binaryValue == null)) { _reportError("Current token ("+_currToken+") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary"); } // To ensure that we won't see inconsistent data, better clear up state... if (_tokenIncomplete) { try { _binaryValue = _decodeBase64(b64variant); } catch (IllegalArgumentException iae) { throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage()); } // let's clear incomplete only now; allows for accessing other textual content in error cases _tokenIncomplete = false; } else { // may actually require conversion... if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } } return _binaryValue; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if (_currToken != JsonToken.VALUE_STRING && (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT || _binaryValue == null)) { _reportError("Current token ("+_currToken+") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary"); } /* To ensure that we won't see inconsistent data, better clear up * state... */ if (_tokenIncomplete) { try { _binaryValue = _decodeBase64(b64variant); } catch (IllegalArgumentException iae) { throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage()); } /* let's clear incomplete only now; allows for accessing other * textual content in error cases */ _tokenIncomplete = false; } else { // may actually require conversion... if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } } return _binaryValue; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if ((_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) && (_binaryValue != null)) { return _binaryValue; } if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token ("+_currToken+") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary"); } // To ensure that we won't see inconsistent data, better clear up state if (_tokenIncomplete) { try { _binaryValue = _decodeBase64(b64variant); } catch (IllegalArgumentException iae) { throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage()); } /* let's clear incomplete only now; allows for accessing other * textual content in error cases */ _tokenIncomplete = false; } else { // may actually require conversion... if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } } return _binaryValue; }
@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(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { if ((_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) && (_binaryValue != null)) { return _binaryValue; } if (_currToken != JsonToken.VALUE_STRING) { _reportError("Current token ("+_currToken+") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary"); } // To ensure that we won't see inconsistent data, better clear up state if (_tokenIncomplete) { try { _binaryValue = _decodeBase64(b64variant); } catch (IllegalArgumentException iae) { throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage()); } // let's clear incomplete only now; allows for accessing other // textual content in error cases _tokenIncomplete = false; } else { // may actually require conversion... if (_binaryValue == null) { @SuppressWarnings("resource") ByteArrayBuilder builder = _getByteArrayBuilder(); _decodeBase64(getText(), builder, b64variant); _binaryValue = builder.toByteArray(); } } return _binaryValue; }
@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); }
@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); }
@Test public void testCanSerialiseAndDeserialiseWithRuns() 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); } testBitmap.runOptimize(); 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); }