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(); }
@Test public void testReadBinaryValueThrowsIOException() { BufferRecycler bufferRecycler = new BufferRecycler(); IOContext ioContext = new IOContext(bufferRecycler, this, false); ByteQuadsCanonicalizer byteQuadsCanonicalizer = ByteQuadsCanonicalizer.createRoot(); UTF8DataInputJsonParser uTF8DataInputJsonParser = new UTF8DataInputJsonParser(ObjectReadContext.empty(), ioContext, (-53), null, byteQuadsCanonicalizer, 48); ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(bufferRecycler, 1); try { uTF8DataInputJsonParser.readBinaryValue(null, byteArrayBuilder); fail("Expecting exception: IOException"); } catch (IOException e) { assertEquals(JsonParser.class.getName(), e.getStackTrace()[0].getClassName()); } }
@Test public void testDecodeTaking2ArgumentsThrowsIllegalArgumentException() { Base64Variant base64Variant = new Base64Variant("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", false, 'x', 'x'); assertEquals(120, base64Variant.getMaxLineLength()); assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", base64Variant.toString()); assertFalse(base64Variant.usesPadding()); assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", base64Variant.getName()); assertEquals((byte) 120, base64Variant.getPaddingByte()); assertEquals('x', base64Variant.getPaddingChar()); ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); try { base64Variant.decode("-%8en$9m=>$m", byteArrayBuilder); fail("Expecting exception: IllegalArgumentException"); } catch (IllegalArgumentException e) { assertEquals(Base64Variant.class.getName(), e.getStackTrace()[0].getClassName()); } }
@Test public void testDecodeTaking2ArgumentsOne() { Base64Variant base64Variant = new Base64Variant("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", false, 'R', 4); BufferRecycler bufferRecycler = new BufferRecycler(); ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(bufferRecycler); base64Variant.decode("PEM", byteArrayBuilder); assertFalse(base64Variant.usesPadding()); assertEquals('R', base64Variant.getPaddingChar()); assertEquals(4, base64Variant.getMaxLineLength()); assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", base64Variant.toString()); assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", base64Variant.getName()); assertEquals((byte) 82, base64Variant.getPaddingByte()); }
@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); }
public byte[] writeValueAsBytes(Object paramObject) { ByteArrayBuilder localByteArrayBuilder = new ByteArrayBuilder(this._jsonFactory._getBufferRecycler()); try { _configAndWriteValue(this._jsonFactory.createGenerator(localByteArrayBuilder, JsonEncoding.UTF8), paramObject); } catch (JsonProcessingException localJsonProcessingException) { throw localJsonProcessingException; } catch (IOException localIOException) { throw JsonMappingException.fromUnexpectedIOE(localIOException); } byte[] arrayOfByte = localByteArrayBuilder.toByteArray(); localByteArrayBuilder.release(); return arrayOfByte; }
public final byte[] getBinaryValue(Base64Variant paramBase64Variant) { if ((this._currToken != JsonToken.VALUE_STRING) && ((this._currToken != JsonToken.VALUE_EMBEDDED_OBJECT) || (this._binaryValue == null))) _reportError("Current token (" + this._currToken + ") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary"); if (this._tokenIncomplete) { try { this._binaryValue = _decodeBase64(paramBase64Variant); } catch (IllegalArgumentException localIllegalArgumentException) { throw _constructError("Failed to decode VALUE_STRING as base64 (" + paramBase64Variant + "): " + localIllegalArgumentException.getMessage()); } this._tokenIncomplete = false; } else if (this._binaryValue == null) { ByteArrayBuilder localByteArrayBuilder = _getByteArrayBuilder(); _decodeBase64(getText(), localByteArrayBuilder, paramBase64Variant); this._binaryValue = localByteArrayBuilder.toByteArray(); } return this._binaryValue; }
private int _appendByteEscape(int ch, int escCode, ByteArrayBuilder byteBuilder, int ptr) { byteBuilder.setCurrentSegmentLength(ptr); byteBuilder.append(INT_BACKSLASH); if (escCode < 0) { // standard escape byteBuilder.append(INT_U); if (ch > 0xFF) { int hi = (ch >> 8); byteBuilder.append(HEX_BYTES[hi >> 4]); byteBuilder.append(HEX_BYTES[hi & 0xF]); ch &= 0xFF; } else { byteBuilder.append(INT_0); byteBuilder.append(INT_0); } byteBuilder.append(HEX_BYTES[ch >> 4]); byteBuilder.append(HEX_BYTES[ch & 0xF]); } else { // 2-char simple escape byteBuilder.append((byte) escCode); } return byteBuilder.getCurrentSegmentLength(); }
@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; }
/** * Helper method that can be used for base64 decoding in cases where * encoded content has already been read as a String. */ protected void _decodeBase64(String str, ByteArrayBuilder builder, Base64Variant b64variant) throws IOException { try { b64variant.decode(str, builder); } catch (IllegalArgumentException e) { _reportError(e.getMessage()); } }
@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; }
/** * Converts a {@link Message} into JSON as UTF-8 encoded bytes. * * @throws InvalidProtocolBufferException if there are unknown Any types in the message. */ public <T extends Message> byte[] writeValueAsBytes(T message) throws IOException { checkNotNull(message, "message"); ByteArrayBuilder builder = new ByteArrayBuilder(jsonFactory._getBufferRecycler()); JsonGenerator gen = jsonFactory.createGenerator(builder); writeValue(message, gen); return builder.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(); }
public final byte[] getBinaryValue(Base64Variant paramBase64Variant) { if (this._currToken == JsonToken.VALUE_EMBEDDED_OBJECT) { Object localObject2 = _currentObject(); if ((localObject2 instanceof byte[])) return (byte[])localObject2; } if (this._currToken != JsonToken.VALUE_STRING) throw _constructError("Current token (" + this._currToken + ") not VALUE_STRING (or VALUE_EMBEDDED_OBJECT with byte[]), can not access as binary"); String str = getText(); if (str == null) return null; ByteArrayBuilder localByteArrayBuilder1 = this._byteBuilder; Object localObject1 = localByteArrayBuilder1; if (localByteArrayBuilder1 == null) { ByteArrayBuilder localByteArrayBuilder2 = new ByteArrayBuilder(100); localObject1 = localByteArrayBuilder2; this._byteBuilder = localByteArrayBuilder2; } else { this._byteBuilder.reset(); } _decodeBase64(str, (ByteArrayBuilder)localObject1, paramBase64Variant); return ((ByteArrayBuilder)localObject1).toByteArray(); }
public void _decodeBase64(String paramString, ByteArrayBuilder paramByteArrayBuilder, Base64Variant paramBase64Variant) { try { paramBase64Variant.decode(paramString, paramByteArrayBuilder); return; } catch (IllegalArgumentException localIllegalArgumentException) { _reportError(localIllegalArgumentException.getMessage()); } }
private int _appendByteEscape(int paramInt1, int paramInt2, ByteArrayBuilder paramByteArrayBuilder, int paramInt3) { paramByteArrayBuilder.setCurrentSegmentLength(paramInt3); paramByteArrayBuilder.append(92); if (paramInt2 < 0) { paramByteArrayBuilder.append(117); if (paramInt1 > 255) { int i = paramInt1 >> 8; paramByteArrayBuilder.append(HEX_BYTES[(i >> 4)]); paramByteArrayBuilder.append(HEX_BYTES[(i & 0xF)]); paramInt1 &= 255; } else { paramByteArrayBuilder.append(48); paramByteArrayBuilder.append(48); } paramByteArrayBuilder.append(HEX_BYTES[(paramInt1 >> 4)]); paramByteArrayBuilder.append(HEX_BYTES[(paramInt1 & 0xF)]); } else { paramByteArrayBuilder.append((byte)paramInt2); } return paramByteArrayBuilder.getCurrentSegmentLength(); }
protected byte[] getLogMessage(final ObjectNode event) { ByteArrayBuilder buf = new ByteArrayBuilder(); try { mapper.writeValue(buf, event); } catch (IOException e) { addError("while serializing log event", e); return NADA; } buf.append('\n'); return buf.toByteArray(); }
/** * Helper method that can be used for base64 decoding in cases where * encoded content has already been read as a String. */ protected void _decodeBase64(String str, ByteArrayBuilder builder, Base64Variant b64variant) throws IOException, JsonParseException { // just call helper method introduced in 2.2.3 try { b64variant.decode(str, builder); } catch (IllegalArgumentException e) { _reportError(e.getMessage()); } }
protected JSONComposer(int features, JsonGenerator gen, ByteArrayBuilder w) { super(gen); _features = features; _stringWriter = null; _byteWriter = w; _closeGenerator = true; }
@Override 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[]), can not 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(); }
public final void decode(String paramString, ByteArrayBuilder paramByteArrayBuilder) { int i = 0; int j = paramString.length(); while (i < j) { char c1; do { int k = i; i++; c1 = paramString.charAt(k); if (i >= j) break; } while (c1 <= ' '); int m = decodeBase64Char(c1); if (m < 0) _reportInvalidBase64(c1, 0, null); if (i >= j) _reportBase64EOF(); int n = i; int i1 = i + 1; char c2 = paramString.charAt(n); int i2 = decodeBase64Char(c2); if (i2 < 0) _reportInvalidBase64(c2, 1, null); int i3 = i2 | m << 6; if (i1 >= j) { if (!usesPadding()) { paramByteArrayBuilder.append(i3 >> 4); return; } _reportBase64EOF(); } int i4 = i1 + 1; char c3 = paramString.charAt(i1); int i5 = decodeBase64Char(c3); if (i5 < 0) { if (i5 != -2) _reportInvalidBase64(c3, 2, null); if (i4 >= j) _reportBase64EOF(); i = i4 + 1; char c5 = paramString.charAt(i4); if (!usesPaddingChar(c5)) _reportInvalidBase64(c5, 3, "expected padding character '" + getPaddingChar() + "'"); paramByteArrayBuilder.append(i3 >> 4); } else { int i6 = i5 | i3 << 6; if (i4 >= j) { if (!usesPadding()) { paramByteArrayBuilder.appendTwoBytes(i6 >> 2); return; } _reportBase64EOF(); } i = i4 + 1; char c4 = paramString.charAt(i4); int i7 = decodeBase64Char(c4); if (i7 < 0) { if (i7 != -2) _reportInvalidBase64(c4, 3, null); paramByteArrayBuilder.appendTwoBytes(i6 >> 2); } else { paramByteArrayBuilder.appendThreeBytes(i7 | i6 << 6); } } } }
public final byte[] decode(String paramString) { ByteArrayBuilder localByteArrayBuilder = new ByteArrayBuilder(); decode(paramString, localByteArrayBuilder); return localByteArrayBuilder.toByteArray(); }
public static JSONComposer<byte[]> bytesComposer(int features, JsonGenerator gen, ByteArrayBuilder w) { return new JSONComposer<byte[]>(features, gen, w); }
public JSONComposer<byte[]> composeBytes() throws IOException, JSONObjectException { ByteArrayBuilder out = new ByteArrayBuilder(_streamFactory._getBufferRecycler()); JsonGenerator gen = _config(_streamFactory.createGenerator(this, out)); return JSONComposer.bytesComposer(_features, gen, out); }
/** * 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(); }