Skip to content

Commit 2a3f8cf

Browse files
committed
Possible fix for #329
1 parent e7c3e55 commit 2a3f8cf

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

jakarta-xmlbind/src/main/java/tools/jackson/module/jakarta/xmlbind/ser/DataHandlerSerializer.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
package tools.jackson.module.jakarta.xmlbind.ser;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
54
import java.io.InputStream;
65

76
import jakarta.activation.DataHandler;
87

98
import tools.jackson.core.*;
109
import tools.jackson.core.exc.JacksonIOException;
11-
10+
import tools.jackson.core.type.WritableTypeId;
1211
import tools.jackson.databind.JavaType;
1312
import tools.jackson.databind.SerializationContext;
1413
import tools.jackson.databind.ser.std.StdSerializer;
1514
import tools.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
1615
import tools.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
1716
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
17+
import tools.jackson.databind.jsontype.TypeSerializer;
1818

1919
public class DataHandlerSerializer extends StdSerializer<DataHandler>
2020
{
2121
public DataHandlerSerializer() { super(DataHandler.class); }
22-
22+
2323
@Override
2424
public void serialize(DataHandler value, JsonGenerator g, SerializationContext ctxt)
2525
throws JacksonException
2626
{
27-
final ByteArrayOutputStream out = new ByteArrayOutputStream();
28-
/* for copy-through, a small buffer should suffice: ideally
29-
* we might want to reuse a generic byte buffer, but for now
30-
* there's no serializer context to hold them.
31-
*
32-
* Also: it'd be nice not to have buffer all data, but use a
33-
* streaming output. But currently JsonGenerator won't allow
34-
* that.
35-
*/
36-
byte[] buffer = new byte[1024 * 4];
27+
_writePayload(value, g, ctxt);
28+
}
3729

30+
// Copied from `jackson-databind` `ByteArraySerializer`
31+
@Override
32+
public void serializeWithType(DataHandler value, JsonGenerator g, SerializationContext ctxt,
33+
TypeSerializer typeSer)
34+
throws JacksonException
35+
{
36+
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
37+
typeSer.typeId(value, JsonToken.VALUE_EMBEDDED_OBJECT));
38+
_writePayload(value, g, ctxt);
39+
typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
40+
}
41+
42+
protected void _writePayload(DataHandler value, JsonGenerator g, SerializationContext ctxt)
43+
{
3844
try (InputStream in = value.getInputStream()) {
39-
int len = in.read(buffer);
40-
while (len > 0) {
41-
out.write(buffer, 0, len);
42-
len = in.read(buffer);
43-
}
45+
g.writeBinary(ctxt.getConfig().getBase64Variant(), in, -1);
4446
} catch (IOException e) {
4547
throw JacksonIOException.construct(e);
4648
}
47-
g.writeBinary(out.toByteArray());
4849
}
4950

5051
@Override

jaxb/src/main/java/tools/jackson/module/jaxb/ser/DataHandlerSerializer.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
11
package tools.jackson.module.jaxb.ser;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
54
import java.io.InputStream;
65

76
import javax.activation.DataHandler;
87

98
import tools.jackson.core.*;
109
import tools.jackson.core.exc.JacksonIOException;
10+
import tools.jackson.core.type.WritableTypeId;
1111
import tools.jackson.databind.JavaType;
1212
import tools.jackson.databind.SerializationContext;
1313
import tools.jackson.databind.ser.std.StdSerializer;
1414
import tools.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
1515
import tools.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
1616
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
17+
import tools.jackson.databind.jsontype.TypeSerializer;
1718

1819
public class DataHandlerSerializer extends StdSerializer<DataHandler>
1920
{
2021
public DataHandlerSerializer() { super(DataHandler.class); }
21-
22+
2223
@Override
2324
public void serialize(DataHandler value, JsonGenerator g, SerializationContext ctxt)
2425
throws JacksonException
2526
{
26-
final ByteArrayOutputStream out = new ByteArrayOutputStream();
27-
/* for copy-through, a small buffer should suffice: ideally
28-
* we might want to reuse a generic byte buffer, but for now
29-
* there's no serializer context to hold them.
30-
*
31-
* Also: it'd be nice not to have buffer all data, but use a
32-
* streaming output. But currently JsonGenerator won't allow
33-
* that.
34-
*/
35-
byte[] buffer = new byte[1024 * 4];
27+
_writePayload(value, g, ctxt);
28+
}
3629

30+
// Copied from `jackson-databind` `ByteArraySerializer`
31+
@Override
32+
public void serializeWithType(DataHandler value, JsonGenerator g, SerializationContext ctxt,
33+
TypeSerializer typeSer)
34+
throws JacksonException
35+
{
36+
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
37+
typeSer.typeId(value, JsonToken.VALUE_EMBEDDED_OBJECT));
38+
_writePayload(value, g, ctxt);
39+
typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
40+
}
41+
42+
protected void _writePayload(DataHandler value, JsonGenerator g, SerializationContext ctxt)
43+
{
3744
try (InputStream in = value.getInputStream()) {
38-
int len = in.read(buffer);
39-
while (len > 0) {
40-
out.write(buffer, 0, len);
41-
len = in.read(buffer);
42-
}
45+
g.writeBinary(ctxt.getConfig().getBase64Variant(), in, -1);
4346
} catch (IOException e) {
4447
throw JacksonIOException.construct(e);
4548
}
46-
g.writeBinary(out.toByteArray());
4749
}
4850

4951
@Override

0 commit comments

Comments
 (0)