-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensuring an unmarshalled InputStream is not closed
Accepting an existing InputStream for consuming, should not assume that the InputStream can be closed.
- Loading branch information
1 parent
ae21124
commit acd6be0
Showing
5 changed files
with
158 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
67 changes: 67 additions & 0 deletions
67
jaxb/src/main/java/no/digipost/xml/transform/sax/ClosePreventingInputSource.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) Posten Norge AS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package no.digipost.xml.transform.sax; | ||
|
||
import org.xml.sax.InputSource; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.nio.charset.Charset; | ||
|
||
|
||
class ClosePreventingInputSource extends InputSource { | ||
|
||
private boolean initialized; | ||
|
||
public ClosePreventingInputSource(InputStream inputStream, Charset encoding) { | ||
super(new ClosePreventingInputStream(inputStream)); | ||
if (encoding != null) { | ||
this.setEncoding(encoding.name()); | ||
} | ||
this.initialized = true; | ||
} | ||
|
||
private static final class ClosePreventingInputStream extends FilterInputStream { | ||
public ClosePreventingInputStream(InputStream in) { | ||
super(in); | ||
} | ||
@Override | ||
public void close() { | ||
// prevent closing | ||
} | ||
} | ||
|
||
|
||
|
||
@Override | ||
public final Reader getCharacterStream() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public final void setCharacterStream(Reader characterStream) { | ||
throw new UnsupportedOperationException("Setting a character stream is not supported"); | ||
} | ||
|
||
@Override | ||
public final void setByteStream(InputStream byteStream) { | ||
if (initialized) { | ||
throw new UnsupportedOperationException("Setting a byte stream is not supported"); | ||
} | ||
super.setByteStream(byteStream); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
jaxb/src/main/java/no/digipost/xml/transform/sax/SaxInputSources.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) Posten Norge AS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package no.digipost.xml.transform.sax; | ||
|
||
import org.xml.sax.InputSource; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public final class SaxInputSources { | ||
|
||
/** | ||
* Create a SAX {@link InputSource} wrapping the given existing InputStream | ||
* which will not be closed by anything consuming the InputSource. | ||
* The wrapped InputStream, which will be returned by {@link InputSource#getByteStream()}, | ||
* will be ensured cannot be closed, even if attempted. The responsibility to | ||
* properly manage an existing InputStream (i.e. closing) | ||
* is outside the InputSource, and it cannot assume that the wrapped stream | ||
* can be closed after done consuming it. | ||
*/ | ||
public static InputSource fromInputStreamPreventClose(InputStream inputStream) { | ||
return fromInputStreamPreventClose(inputStream, null); | ||
} | ||
|
||
/** | ||
* Create a SAX {@link InputSource} wrapping the given existing InputStream | ||
* which will not be closed by anything consuming the InputSource. | ||
* | ||
* @see #fromInputStreamPreventClose(InputStream) | ||
*/ | ||
public static InputSource fromInputStreamPreventClose(InputStream inputStream, Charset encoding) { | ||
return new ClosePreventingInputSource(inputStream, encoding); | ||
} | ||
|
||
|
||
public static InputSource fromClasspath(String resourceName) { | ||
return fromClasspath(resourceName, null); | ||
} | ||
|
||
public static InputSource fromClasspath(String resourceName, Charset encoding) { | ||
return fromClasspath(resourceName, encoding, UrlInputSource.class.getClassLoader()); | ||
} | ||
|
||
public static InputSource fromClasspath(String resourceName, Charset encoding, ClassLoader classLoader) { | ||
URL location = classLoader.getResource(resourceName.startsWith("/") ? resourceName.substring(1) : resourceName); | ||
requireNonNull(location, resourceName + " not found on classpath"); | ||
return new UrlInputSource(location, encoding); | ||
} | ||
|
||
private SaxInputSources() { | ||
} | ||
} |
This file contains 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
This file contains 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