Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.6k
[BEAM-171] Fix JAXBCoder in the nested context#116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a11010befa109c92edd88bb32b59b4afa46e34ac00c792577File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,6 +19,8 @@ | ||
| import com.google.cloud.dataflow.sdk.util.CloudObject; | ||
| import com.google.cloud.dataflow.sdk.util.Structs; | ||
| import com.google.cloud.dataflow.sdk.util.VarInt; | ||
| import com.google.common.io.ByteStreams; | ||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| @@ -71,13 +73,19 @@ public void encode(T value, OutputStream outStream, Context context) | ||
| JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass); | ||
| jaxbMarshaller = jaxbContext.createMarshaller(); | ||
| } | ||
| jaxbMarshaller.marshal(value, new FilterOutputStream(outStream) { | ||
| // JAXB closes the underyling stream so we must filter out those calls. | ||
| @Override | ||
| public void close() throws IOException { | ||
| if (!context.isWholeStream) { | ||
| try { | ||
| long size = getEncodedElementByteSize(value, Context.OUTER); | ||
| // record the number of bytes the XML consists of so when reading we only read the encoded | ||
| // value | ||
| VarInt.encode(size, outStream); | ||
| } catch (Exception e) { | ||
| throw new CoderException( | ||
| "An Exception occured while trying to get the size of an encoded representation", e); | ||
| } | ||
| }); | ||
| } | ||
| jaxbMarshaller.marshal(value, new CloseIgnoringOutputStream(outStream)); | ||
| } catch (JAXBException e) { | ||
| throw new CoderException(e); | ||
| } | ||
| @@ -91,13 +99,13 @@ public T decode(InputStream inStream, Context context) throws CoderException, IO | ||
| jaxbUnmarshaller = jaxbContext.createUnmarshaller(); | ||
| } | ||
| InputStream stream = inStream; | ||
| if (!context.isWholeStream) { | ||
| long limit = VarInt.decodeLong(inStream); | ||
| stream = ByteStreams.limit(inStream, limit); | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| T obj = (T) jaxbUnmarshaller.unmarshal(new FilterInputStream(inStream) { | ||
| // JAXB closes the underyling stream so we must filter out those calls. | ||
| @Override | ||
| public void close() throws IOException { | ||
| } | ||
| }); | ||
| T obj = (T) jaxbUnmarshaller.unmarshal(new CloseIgnoringInputStream(stream)); | ||
| return obj; | ||
| } catch (JAXBException e) { | ||
| throw new CoderException(e); | ||
| @@ -109,6 +117,30 @@ public String getEncodingId() { | ||
| return getJAXBClass().getName(); | ||
| } | ||
| private static class CloseIgnoringInputStream extends FilterInputStream { | ||
| protected CloseIgnoringInputStream(InputStream in) { | ||
| super(in); | ||
| } | ||
| @Override | ||
| public void close() { | ||
| // Do nothing. JAXB closes the underlying stream so we must filter out those calls. | ||
| } | ||
| } | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove newline. Also, I was wrong about flush -- no need to flush an input stream (duh) MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. | ||
| private static class CloseIgnoringOutputStream extends FilterOutputStream { | ||
| protected CloseIgnoringOutputStream(OutputStream out) { | ||
| super(out); | ||
| } | ||
| @Override | ||
| public void close() throws IOException { | ||
| // JAXB closes the underlying stream so we must filter out those calls. | ||
| } | ||
| } | ||
| //////////////////////////////////////////////////////////////////////////////////// | ||
| // JSON Serialization details below | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about extracting the common core for simplicity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.