-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a11010b
Fix JAXBCoder in the nested context
tgroh efa109c
fixup! Fix JAXBCoder in the nested context
tgroh 92edd88
fixup! Fix JAXBCoder in the nested context
tgroh bb32b59
fixup! Fix JAXBCoder in the nested context
tgroh b4afa46
fixup! Fix JAXBCoder in the nested context
tgroh e34ac00
fixup! Fix JAXBCoder in the nested context
tgroh c792577
fixup! Fix JAXBCoder in the nested context
tgroh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)
Member
Author
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 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.