|
1 | 1 | package tools.jackson.module.jakarta.xmlbind.ser; |
2 | 2 |
|
3 | | -import java.io.ByteArrayOutputStream; |
4 | 3 | import java.io.IOException; |
5 | 4 | import java.io.InputStream; |
6 | 5 |
|
7 | 6 | import jakarta.activation.DataHandler; |
8 | 7 |
|
9 | 8 | import tools.jackson.core.*; |
10 | 9 | import tools.jackson.core.exc.JacksonIOException; |
11 | | - |
| 10 | +import tools.jackson.core.type.WritableTypeId; |
12 | 11 | import tools.jackson.databind.JavaType; |
13 | 12 | import tools.jackson.databind.SerializationContext; |
14 | 13 | import tools.jackson.databind.ser.std.StdSerializer; |
15 | 14 | import tools.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor; |
16 | 15 | import tools.jackson.databind.jsonFormatVisitors.JsonFormatTypes; |
17 | 16 | import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; |
| 17 | +import tools.jackson.databind.jsontype.TypeSerializer; |
18 | 18 |
|
19 | 19 | public class DataHandlerSerializer extends StdSerializer<DataHandler> |
20 | 20 | { |
21 | 21 | public DataHandlerSerializer() { super(DataHandler.class); } |
22 | | - |
| 22 | + |
23 | 23 | @Override |
24 | 24 | public void serialize(DataHandler value, JsonGenerator g, SerializationContext ctxt) |
25 | 25 | throws JacksonException |
26 | 26 | { |
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 | + } |
37 | 29 |
|
| 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 | + { |
38 | 44 | 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); |
44 | 46 | } catch (IOException e) { |
45 | 47 | throw JacksonIOException.construct(e); |
46 | 48 | } |
47 | | - g.writeBinary(out.toByteArray()); |
48 | 49 | } |
49 | 50 |
|
50 | 51 | @Override |
|
0 commit comments