use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project jackson-databind by FasterXML.
the class TextNode method getBinaryValue.
/**
* 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("Can not access contents of TextNode as binary due to broken Base64 encoding: %s", e.getMessage()), str, byte[].class);
}
return builder.toByteArray();
}
use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project jackson-core by FasterXML.
the class JsonStringEncoder method quoteAsUTF8.
/**
* Will quote given JSON String value using standard quoting, encode
* results as UTF-8, and return result as a byte array.
*/
@SuppressWarnings("resource")
public byte[] quoteAsUTF8(String text) {
ByteArrayBuilder bb = _bytes;
if (bb == null) {
// no allocator; can add if we must, shouldn't need to
_bytes = bb = new ByteArrayBuilder(null);
}
int inputPtr = 0;
int inputEnd = text.length();
int outputPtr = 0;
byte[] outputBuffer = bb.resetAndGetFirstSegment();
main: while (inputPtr < inputEnd) {
final int[] escCodes = CharTypes.get7BitOutputEscapes();
inner_loop: while (true) {
int ch = text.charAt(inputPtr);
if (ch > 0x7F || escCodes[ch] != 0) {
break inner_loop;
}
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
outputBuffer[outputPtr++] = (byte) ch;
if (++inputPtr >= inputEnd) {
break main;
}
}
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
// Ok, so what did we hit?
int ch = (int) text.charAt(inputPtr++);
if (ch <= 0x7F) {
// needs quoting
int escape = escCodes[ch];
// ctrl-char, 6-byte escape...
outputPtr = _appendByte(ch, escape, bb, outputPtr);
outputBuffer = bb.getCurrentSegment();
continue main;
}
if (ch <= 0x7FF) {
// fine, just needs 2 byte output
outputBuffer[outputPtr++] = (byte) (0xc0 | (ch >> 6));
ch = (0x80 | (ch & 0x3f));
} else {
// Surrogates?
if (ch < SURR1_FIRST || ch > SURR2_LAST) {
// nope
outputBuffer[outputPtr++] = (byte) (0xe0 | (ch >> 12));
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
outputBuffer[outputPtr++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
ch = (0x80 | (ch & 0x3f));
} else {
// yes, surrogate pair
if (ch > SURR1_LAST) {
// must be from first range
_illegal(ch);
}
// and if so, followed by another from next range
if (inputPtr >= inputEnd) {
_illegal(ch);
}
ch = _convert(ch, text.charAt(inputPtr++));
if (ch > 0x10FFFF) {
// illegal, as per RFC 4627
_illegal(ch);
}
outputBuffer[outputPtr++] = (byte) (0xf0 | (ch >> 18));
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
outputBuffer[outputPtr++] = (byte) (0x80 | ((ch >> 12) & 0x3f));
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
outputBuffer[outputPtr++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
ch = (0x80 | (ch & 0x3f));
}
}
if (outputPtr >= outputBuffer.length) {
outputBuffer = bb.finishCurrentSegment();
outputPtr = 0;
}
outputBuffer[outputPtr++] = (byte) ch;
}
return _bytes.completeAndCoalesce(outputPtr);
}
use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialise.
@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);
}
use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanDeserialiseVersionZeroPointThreePointFourBitmap.
@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);
}
use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialiseWithRuns.
@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);
}
Aggregations