public ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
public ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
/** * 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 * * @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(); }
/** * 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(); }
/** * 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; }
/** * 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; }
@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(); }
if (byteBuilder == null) { _bytes = byteBuilder = new ByteArrayBuilder(null);
if (bb == null) { _bytes = bb = new ByteArrayBuilder(null);
if (byteBuilder == null) { _bytes = byteBuilder = new ByteArrayBuilder(null);
@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); }
if (bb == null) { _bytes = bb = new ByteArrayBuilder(null);
@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); }
protected ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
public ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
protected ByteArrayBuilder _getByteArrayBuilder() { if (_byteArrayBuilder == null) { _byteArrayBuilder = new ByteArrayBuilder(); } else { _byteArrayBuilder.reset(); } return _byteArrayBuilder; }
public byte[] writeEventAsBytes(Event event) throws IOException { ByteArrayBuilder outputStream = new ByteArrayBuilder(getBufferRecycler()); try { writeEventToOutputStream(event, outputStream); outputStream.flush(); return outputStream.toByteArray(); } finally { outputStream.release(); } }