下面列出了怎么用org.apache.commons.io.input.CloseShieldInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
private String addParamsFromRequestBody(String prev, InputStream is) {
if (null == is) {
return prev;
}
try {
CloseShieldInputStream cloned = new CloseShieldInputStream(is);
String body = FileTextReader.getText(cloned);
int bodyLength = body.length();
if (bodyLength > 0) {
byte[] bytes = body.getBytes();
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> params;
params = objectMapper.readValue(bytes, HashMap.class);
return this.addParameters(prev, params);
}
return prev;
} catch (Exception e) {
logger.error("System exception {}", e.getMessage(), e);
return prev;
}
}
public InputStreamPump(InputStream source, final Pump pump) throws IOException {
this.source = source;
out = new PipedOutputStream();
in = new PipedInputStream(out, 8192);
pumpThread = new Thread(new Runnable() {
public void run() {
try {
pump.run(new CloseShieldInputStream(in));
// ensure that input stream is pumping in case it didn't read to the end
byte[] buffer = new byte[8192];
while (in.read(buffer) >= 0);
} catch (Exception e) {
error = e;
log.error("Error while processing input stream", e);
}
}
});
pumpThread.start();
}
protected Collection<ValidationMessage> validateDocumentViewXml(InputStream input, @NotNull Path filePath, @NotNull Path basePath, String rootNodePath,
Map<String, Integer> nodePathsAndLineNumbers) throws IOException, SAXException {
List<ValidationMessage> enrichedMessages = new LinkedList<>();
XMLReader xr = saxParser.getXMLReader();
final DocumentViewXmlContentHandler handler = new DocumentViewXmlContentHandler(filePath, basePath, rootNodePath,
docViewValidators);
enrichedMessages.add(new ValidationMessage(ValidationMessageSeverity.DEBUG, "Detected DocView..."));
xr.setContentHandler(handler);
try {
xr.parse(new InputSource(new CloseShieldInputStream(input)));
enrichedMessages.addAll(ValidationViolation.wrapMessages(null, handler.getViolations(), filePath, basePath, rootNodePath, 0, 0));
} catch (SAXException e) {
enrichedMessages.add(new ValidationViolation(severity, "Invalid XML found: " + e.getMessage(), filePath, basePath, rootNodePath, 0, 0, e));
}
nodePathsAndLineNumbers.putAll(handler.getNodePaths());
return enrichedMessages;
}
public FixedLengthInputStream(InputStream stream, long maxLen) {
super(new CountingInputStream(new CloseShieldInputStream(stream)));
// Save a correctly-typed reference to the underlying stream.
this.countingIn = (CountingInputStream) this.in;
this.maxBytes = maxLen;
}
@Override
public InputStream getStream() throws IOException {
// we explicitly protect this servlet stream from being closed
// so that it does not trip our test assert in our close shield
// in SolrDispatchFilter - we must allow closes from getStream
// due to the other impls of ContentStream
return new CloseShieldInputStream(req.getInputStream());
}
public SolrParams parseParamsAndFillStreams(HttpServletRequest req, ArrayList<ContentStream> streams, InputStream in) throws Exception {
final Map<String,String[]> map = new HashMap<>();
// also add possible URL parameters and include into the map (parsed using UTF-8):
final String qs = req.getQueryString();
if (qs != null) {
parseQueryString(qs, map);
}
// may be -1, so we check again later. But if it's already greater we can stop processing!
final long totalLength = req.getContentLength();
final long maxLength = ((long) uploadLimitKB) * 1024L;
if (totalLength > maxLength) {
throw new SolrException(ErrorCode.BAD_REQUEST, "application/x-www-form-urlencoded content length (" +
totalLength + " bytes) exceeds upload limit of " + uploadLimitKB + " KB");
}
// get query String from request body, using the charset given in content-type:
final String cs = ContentStreamBase.getCharsetFromContentType(req.getContentType());
final Charset charset = (cs == null) ? StandardCharsets.UTF_8 : Charset.forName(cs);
try {
// Protect container owned streams from being closed by us, see SOLR-8933
in = FastInputStream.wrap( in == null ? new CloseShieldInputStream(req.getInputStream()) : in );
final long bytesRead = parseFormDataContent(in, maxLength, charset, map, false);
if (bytesRead == 0L && totalLength > 0L) {
throw getParameterIncompatibilityException();
}
} catch (IOException ioe) {
throw new SolrException(ErrorCode.BAD_REQUEST, ioe);
} catch (IllegalStateException ise) {
throw (SolrException) getParameterIncompatibilityException().initCause(ise);
} finally {
IOUtils.closeWhileHandlingException(in);
}
return new MultiMapSolrParams(map);
}
@VisibleForTesting
static void readStream(final InputStream stream, final Consumer<String> lineParser)
throws IOException {
try (Reader inputStreamReader =
new InputStreamReader(new CloseShieldInputStream(stream), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
reader.lines().filter(current -> current.trim().length() != 0).forEachOrdered(lineParser);
} catch (final IllegalArgumentException e) {
throw new IOException(e);
}
}
@Override
public Void call() throws Exception {
final DocumentFactory factory = new DocumentFactory().configure(options);
try (final InputStream input = new CloseShieldInputStream(System.in);
final ReportMap reportMap = new ReportMapFactory(options)
.withDocumentFactory(factory)
.createShared()) {
load(factory, reportMap, input);
}
return null;
}
@Override
public Void call() throws Exception {
final DocumentFactory factory = new DocumentFactory().configure(options);
try (final InputStream input = new CloseShieldInputStream(System.in);
final DocumentQueue queue = new DocumentQueueFactory(options)
.withDocumentFactory(factory)
.createShared()) {
load(factory, queue, input);
}
return null;
}
private void processMetaData ( final Context context, final InputStream in, final String filename, final String xpath ) throws Exception
{
// parse input
final Document doc = this.xml.newDocumentBuilder ().parse ( new CloseShieldInputStream ( in ) );
final XPathExpression path = this.xml.newXPathFactory ().newXPath ().compile ( xpath );
// filter
final NodeList result = XmlHelper.executePath ( doc, path );
// write filtered output
final Document fragmentDoc = this.xml.newDocumentBuilder ().newDocument ();
Node node = result.item ( 0 );
node = fragmentDoc.adoptNode ( node );
fragmentDoc.appendChild ( node );
// create artifact
context.createVirtualArtifact ( filename, out -> {
try
{
XmlHelper.write ( this.xml.newTransformerFactory (), fragmentDoc, new StreamResult ( out ) );
}
catch ( final Exception e )
{
throw new IOException ( e );
}
}, null );
}
/**
* <p>The specified stream remains open after this method returns.
* @param in
* @param systemId
* @throws IOException
*/
public void loadProperties(@NotNull InputStream in, @NotNull String systemId)
throws IOException {
Properties props = new Properties();
// prevent the input stream from being closed for achieving a consistent behaviour
props.loadFromXML(new CloseShieldInputStream(in));
setProperties(props);
log.trace("Loaded properties from {}.", systemId);
}
/** @param input the given input stream must be reset later on
* @param path
* @return either the path of the root node of the given docview xml or {@code null} if no docview xml given
* @throws IOException */
private static Path getDocumentViewXmlRootPath(BufferedInputStream input, Path path) throws IOException {
Path name = path.getFileName();
Path rootPath = null;
int nameCount = path.getNameCount();
if (name.equals(Paths.get(Constants.DOT_CONTENT_XML))) {
if (nameCount > 1) {
rootPath = path.subpath(0, nameCount - 1);
} else {
rootPath = Paths.get("");
}
// correct suffix matching
} else if (name.toString().endsWith(".xml")) {
// we need to rely on a buffered input stream to be able to reset it later
input.mark(1024);
// analyze content
// this closes the input source internally, therefore protect against closing
// make sure to initialize the SLF4J logger appropriately (for the XmlAnalyzer)
try {
SerializationType type = XmlAnalyzer.analyze(new InputSource(new CloseShieldInputStream(input)));
if (type == SerializationType.XML_DOCVIEW) {
// remove .xml extension
String fileName = path.getFileName().toString();
fileName = fileName.substring(0, fileName.length() - ".xml".length());
if (nameCount > 1) {
rootPath = path.subpath(0, nameCount - 1).resolve(fileName);
} else {
rootPath = Paths.get(fileName);
}
}
} finally {
input.reset();
}
}
return rootPath;
}
private T generateConfigFromUserPrompt() {
System.out.println("Please enter the following fields. Default values are in [].");
System.out.println("To use the default value, hit return.");
T config = null;
try (Scanner s = new Scanner(new CloseShieldInputStream(System.in))) {
// Finish setting up the scanner
s.useDelimiter(System.lineSeparator());
// Determine if the user wants to change advanced configuration
boolean useAdvancedSettings = promptUserForAdvancedSettings(s);
// Load fields dynamically from the provided configuration class
config = clazz.newInstance();
ArrayList<Entry<Field, Prompt>> fields = getFields(useAdvancedSettings);
for (Entry<Field, Prompt> entry : fields) {
Field field = entry.getKey();
String message = entry.getValue().message();
field.setAccessible(true);
System.out.printf("%s [%s]: ", message, field.get(config));
String input = s.nextLine();
if (!input.isEmpty()) {
if (field.getType() == java.lang.Integer.TYPE) {
field.set(config, Integer.valueOf(input));
} else {
field.set(config, input);
}
}
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return config;
}
/** Parses the given InputStream as XML, disabling any external entities with secure processing enabled.
* The given InputStream is not closed. */
public static Document parseUntrustedXML(Logger log, InputStream in) throws SAXException, IOException {
return getUntrustedDocumentBuilder(log).parse(new CloseShieldInputStream(in), SYSTEMID_UNTRUSTED);
}
protected static Properties getPropertiesMap(InputStream input) throws InvalidPropertiesFormatException, IOException {
Properties propertyMap = new Properties();
propertyMap.loadFromXML(new CloseShieldInputStream(input));
return propertyMap;
}
/**
* Archive input stream needs to be wrapped to prevent it from being closed.
*
* @param is
* @return
* @throws IOException
*/
protected InputStream wrap(InputStream is) throws IOException {
return new CloseShieldInputStream(is);
}