Skip to content

Commit

Permalink
Fix #90
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 27, 2019
1 parent a4468c0 commit 9ba6852
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 43 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ Andrey Somov (asomov@github)
* Contributed #101: Use latest SnakeYAML version 1.23 and get rid of deprecated methods
(2.10.0)

Tanguy Leroux (tlrx@github)

* Reported #90: Exception when decoding Jackson-encoded `Base64` binary value in YAML
(2.10.0)
6 changes: 4 additions & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Modules:

2.10.0 (not yet released)

#101: Use latest SnakeYAML version 1.23 and get rid of deprecated methods
#90: Exception when decoding Jackson-encoded `Base64` binary value in YAML
(reported by Tanguy L)
#101: (yaml) Use latest SnakeYAML version 1.23 and get rid of deprecated methods
(contributed by Andrey S)
#108: Add new `CsvParser.Feature.ALLOW_COMMENTS` to replace deprecated
#108: (yaml) Add new `CsvParser.Feature.ALLOW_COMMENTS` to replace deprecated
`JsonParser.Feature.ALLOW_YAML_COMMENTS`

2.9.9 (not yet released)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ private void _writeScalarBinary(Base64Variant b64variant,
if (b64variant == Base64Variants.getDefaultVariant()) {
b64variant = Base64Variants.MIME;
}
String encoded = b64variant.encode(data);
final String lf = _lf();
String encoded = _base64encode(b64variant, data, lf);
_emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded,
null, null, STYLE_BASE64));
}
Expand All @@ -855,4 +856,42 @@ protected ScalarEvent _scalarEvent(String value, DumperOptions.ScalarStyle style
return new ScalarEvent(anchor, yamlTag, NO_TAGS, value,
null, null, style);
}

// // // 26-Feb-2019, tatu: Copied temporarily (for 2.10) from `Base64Variant` to prevent
// // // hard dependency for same minor version

private String _base64encode(final Base64Variant b64v, final byte[] input, final String linefeed)
{
final int inputEnd = input.length;
final StringBuilder sb = new StringBuilder(inputEnd + (inputEnd >> 2) + (inputEnd >> 3));

int chunksBeforeLF = b64v.getMaxLineLength() >> 2;

int inputPtr = 0;
int safeInputEnd = inputEnd-3;

while (inputPtr <= safeInputEnd) {
int b24 = ((int) input[inputPtr++]) << 8;
b24 |= ((int) input[inputPtr++]) & 0xFF;
b24 = (b24 << 8) | (((int) input[inputPtr++]) & 0xFF);
b64v.encodeBase64Chunk(sb, b24);
if (--chunksBeforeLF <= 0) {
sb.append(linefeed);
chunksBeforeLF = b64v.getMaxLineLength() >> 2;
}
}
int inputLeft = inputEnd - inputPtr;
if (inputLeft > 0) {
int b24 = ((int) input[inputPtr++]) << 16;
if (inputLeft == 2) {
b24 |= (((int) input[inputPtr++]) & 0xFF) << 8;
}
b64v.encodeBase64Partial(sb, b24, inputLeft);
}
return sb.toString();
}

protected String _lf() {
return _outputOptions.getLineBreak().getString();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.fasterxml.jackson.dataformat.yaml.deser;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import org.junit.Assert;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
Expand Down Expand Up @@ -41,4 +45,29 @@ public void testBinaryViaTree() throws Exception
final byte[] expectedFileHeader = new byte[]{'G', 'I', 'F', '8', '9', 'a'};
Assert.assertArrayEquals(expectedFileHeader, actualFileHeader);
}

// [dataformats-text#90]
public void testReadLongBinary() throws Exception {
final byte[] data = new byte[1000];
new Random(1234).nextBytes(data);

ByteArrayOutputStream os = new ByteArrayOutputStream();

try (JsonGenerator gen = MAPPER.getFactory().createGenerator(os)) {
gen.writeStartObject();
gen.writeBinaryField("data", data);
gen.writeEndObject();
gen.close();
}

try (JsonParser parser = MAPPER.getFactory().createParser(os.toByteArray())) {
assertEquals(JsonToken.START_OBJECT, parser.nextToken());
assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("data", parser.currentName());
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
Assert.assertArrayEquals(data, parser.getBinaryValue());
assertEquals(JsonToken.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.fasterxml.jackson.dataformat.yaml.ser;

import java.io.StringWriter;
import java.util.Arrays;

import org.junit.Assert;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -26,4 +30,28 @@ public void testBinaryViaTree() throws Exception
final byte[] b = data.binaryValue();
Assert.assertArrayEquals(srcPayload, b);
}

public void testWriteLongBinary() throws Exception {
final int length = 200;
final byte[] data = new byte[length];
Arrays.fill(data, (byte) 1);

StringWriter w = new StringWriter();

try (JsonGenerator gen = MAPPER.getFactory().createGenerator(w)) {
gen.writeStartObject();
gen.writeBinaryField("array", data);
gen.writeEndObject();
gen.close();
}

String yaml = w.toString();
Assert.assertEquals("---\n" +
"array: !!binary |-\n" +
" AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" +
" AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" +
" AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\n" +
" AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=\n", yaml);

}
}

0 comments on commit 9ba6852

Please sign in to comment.