List of usage examples for java.io InputStream markSupported
public boolean markSupported()
mark
and reset
methods. From source file:com.baidubce.services.bos.BosClient.java
private RestartableInputStream wrapRestartableInputStream(InputStream input) { if (input.markSupported()) { return new RestartableResettableInputStream(input); } else {//from w ww .jav a2s.co m return new RestartableNonResettableInputStream(input, this.getStreamBufferSize()); } }
From source file:com.ksc.http.KSCHttpClient.java
/** * Used to perform a last reset on the content input stream (if mark-supported); this is so that, for backward * compatibility reason, any "blind" retry (ie without calling reset) by user of this library with the same input * stream (such as ByteArrayInputStream) could still succeed. * * @param t the failure/* w w w.j a v a 2 s . co m*/ * @param req the request, if known; or null otherwise. * @return the failure as given */ private <T extends Throwable> T lastReset(final T t, final Request<?> req) { try { InputStream content = req.getContent(); if (content != null) { if (content.markSupported()) content.reset(); } } catch (Exception ex) { log.debug("FYI: failed to reset content inputstream before throwing up", ex); } return t; }
From source file:com.ksc.http.KSCHttpClient.java
/** * Reset the input stream of the request before a retry. * * @param request Request containing input stream to reset * @throws ResetException If Input Stream can't be reset which means the request can't be retried *///from w w w. jav a 2 s . c o m private void resetRequestInputStream(final Request<?> request) throws ResetException { InputStream requestInputStream = request.getContent(); if (requestInputStream != null) { if (requestInputStream.markSupported()) { try { requestInputStream.reset(); } catch (IOException ex) { throw new ResetException("Failed to reset the request input stream", ex); } } } }
From source file:com.ksc.http.KSCHttpClient.java
/** * Publishes the "request content length" event, and returns an input stream, which will be made mark-and-resettable * if possible, for progress tracking purposes. * * @return an input stream, which will be made mark-and-resettable if possible, for progress tracking purposes; or * null if the request doesn't have an input stream */// w w w .j a va2 s . co m private InputStream beforeRequest(Request<?> request) { final KscWebServiceRequest kscreq = request.getOriginalRequest(); ProgressListener listener = kscreq.getGeneralProgressListener(); Map<String, String> headers = request.getHeaders(); String s = headers.get("Content-Length"); if (s != null) { try { long contentLength = Long.parseLong(s); publishRequestContentLength(listener, contentLength); } catch (NumberFormatException e) { log.warn("Cannot parse the Content-Length header of the request."); } } InputStream content = request.getContent(); if (content == null) return null; if (!content.markSupported()) { // try to wrap the content input stream to become // mark-and-resettable for signing and retry purposes. if (content instanceof FileInputStream) { try { // ResettableInputStream supports mark-and-reset without // memory buffering content = new ResettableInputStream((FileInputStream) content); } catch (IOException e) { if (log.isDebugEnabled()) log.debug("For the record; ignore otherwise", e); } } } if (!content.markSupported()) content = new SdkBufferedInputStream(content); final InputStream is = ProgressInputStream.inputStreamForRequest(content, kscreq); if (KSCHttpClient.unreliableTestConfig == null) return is; return new UnreliableFilterInputStream(is, unreliableTestConfig.isFakeIOException()) .withBytesReadBeforeException(unreliableTestConfig.getBytesReadBeforeException()) .withMaxNumErrors(unreliableTestConfig.getMaxNumErrors()) .withResetIntervalBeforeException(unreliableTestConfig.getResetIntervalBeforeException()); }
From source file:org.rssowl.core.internal.connection.DefaultProtocolHandler.java
private InputStream pipeStream(InputStream inputStream, HttpMethodBase method) throws IOException { Assert.isNotNull(inputStream);/*from w w w . ja v a2 s . co m*/ /* Retrieve the Content Encoding */ String contentEncoding = method.getResponseHeader(HEADER_RESPONSE_CONTENT_ENCODING) != null ? method.getResponseHeader(HEADER_RESPONSE_CONTENT_ENCODING).getValue() : null; boolean isGzipStream = false; /* * Return in case the Content Encoding is not given and the InputStream does * not support mark() and reset() */ if ((contentEncoding == null || !contentEncoding.equals("gzip")) && !inputStream.markSupported()) //$NON-NLS-1$ return inputStream; /* Content Encoding is set to gzip, so use the GZipInputStream */ if (contentEncoding != null && contentEncoding.equals("gzip")) { //$NON-NLS-1$ isGzipStream = true; } /* Detect if the Stream is gzip encoded */ else if (inputStream.markSupported()) { inputStream.mark(2); int id1 = inputStream.read(); int id2 = inputStream.read(); inputStream.reset(); /* Check for GZip Magic Numbers (See RFC 1952) */ if (id1 == 0x1F && id2 == 0x8B) isGzipStream = true; } /* Create the GZipInputStream then */ if (isGzipStream) { try { return new GZIPInputStream(inputStream); } catch (IOException e) { return inputStream; } } return inputStream; }
From source file:com.ksc.http.KSCHttpClient.java
/** * Internal method to execute the HTTP method given. *//* w w w .j a v a2 s .c o m*/ private <T> Response<T> executeHelper(final Request<?> request, HttpResponseHandler<KscWebServiceResponse<T>> responseHandler, HttpResponseHandler<KscServiceException> errorResponseHandler, final ExecutionContext executionContext, List<RequestHandler2> requestHandlers) throws InterruptedException { /* * add the service endpoint to the logs. You can infer service name from service endpoint */ final KscRequestMetrics kscRequestMetrics = executionContext.getKscRequestMetrics() .addPropertyWith(Field.ServiceName, request.getServiceName()) .addPropertyWith(Field.ServiceEndpoint, request.getEndpoint()); // Make a copy of the original request params and headers so that we can // permute it in this loop and start over with the original every time. final Map<String, List<String>> originalParameters = new LinkedHashMap<String, List<String>>( request.getParameters()); final Map<String, String> originalHeaders = new HashMap<String, String>(request.getHeaders()); // Always mark the input stream before execution. final ExecOneRequestParams execOneParams = new ExecOneRequestParams(); final InputStream originalContent = request.getContent(); if (originalContent != null && originalContent.markSupported() && !(originalContent instanceof BufferedInputStream)) { // Mark only once for non-BufferedInputStream KscWebServiceRequest kscreq = request.getOriginalRequest(); final int readLimit = kscreq.getRequestClientOptions().getReadLimit(); originalContent.mark(readLimit); } while (true) { checkInterrupted(); if (originalContent instanceof BufferedInputStream && originalContent.markSupported()) { // Mark everytime for BufferedInputStream, since the marker could // have been invalidated KscWebServiceRequest kscreq = request.getOriginalRequest(); final int readLimit = kscreq.getRequestClientOptions().getReadLimit(); originalContent.mark(readLimit); } execOneParams.initPerRetry(); if (execOneParams.redirectedURI != null) { /* * [scheme:][//authority][path][?query][#fragment] */ String scheme = execOneParams.redirectedURI.getScheme(); String beforeAuthority = scheme == null ? "" : scheme + "://"; String authority = execOneParams.redirectedURI.getAuthority(); String path = execOneParams.redirectedURI.getPath(); request.setEndpoint(URI.create(beforeAuthority + authority)); request.setResourcePath(path); } if (execOneParams.authRetryParam != null) { request.setEndpoint(execOneParams.authRetryParam.getEndpointForRetry()); } kscRequestMetrics.setCounter(Field.RequestCount, execOneParams.requestCount); if (execOneParams.isRetry()) { request.setParameters(originalParameters); request.setHeaders(originalHeaders); request.setContent(originalContent); } try { Response<T> response = executeOneRequest(request, responseHandler, errorResponseHandler, executionContext, kscRequestMetrics, execOneParams, requestHandlers); if (response != null) { return response; } } catch (IOException ioe) { if (log.isInfoEnabled()) { log.info("Unable to execute HTTP request: " + ioe.getMessage(), ioe); } captureExceptionMetrics(ioe, kscRequestMetrics); kscRequestMetrics.addProperty(Field.KSCRequestID, null); KscClientException ace = new KscClientException( "Unable to execute HTTP request: " + ioe.getMessage(), ioe); if (!shouldRetry(request.getOriginalRequest(), execOneParams, ace, executionContext)) { throw lastReset(ace, request); } // Cache the retryable exception execOneParams.retriedException = ace; } catch (RuntimeException e) { throw lastReset(captureExceptionMetrics(e, kscRequestMetrics), request); } catch (Error e) { throw lastReset(captureExceptionMetrics(e, kscRequestMetrics), request); } finally { /* * Some response handlers need to manually manage the HTTP connection and will take * care of releasing the connection on their own, but if this response handler * doesn't need the connection left open, we go ahead and release the it to free up * resources. */ if (!execOneParams.leaveHttpConnectionOpen) { if (execOneParams.apacheResponse != null) { HttpEntity entity = execOneParams.apacheResponse.getEntity(); if (entity != null) { try { closeQuietly(entity.getContent(), log); } catch (IOException e) { log.warn("Cannot close the response content.", e); } } } } } } /* end while (true) */ }
From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java
/** * Unmarshal XML data from the specified InputStream and return the * resulting content tree. Validation event location information may be * incomplete when using this form of the unmarshal API. * * <p>//from w w w . ja v a 2 s . co m * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. * * @param is * the InputStream to unmarshal XML data from * @return the newly created root object of the java content tree * * @throws JAXBException * If any unexpected errors occur while unmarshalling */ public E unmarshal(java.io.InputStream is) throws JAXBException { try { /* To avoid possible XML External Entity Injection attack, * we need to configure the processor. * * We use STAX XMLInputFactory to do that. * * createXMLStreamReader(is) is 40% slower than unmarshal(is). * * But it seems to be the best we can do ... * * org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is) * unmarshal(doc) * * ie DOM is 5x slower than unmarshal(is) * */ XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception XMLStreamReader xsr = xif.createXMLStreamReader(is); Unmarshaller u = jc.createUnmarshaller(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); if (is.markSupported()) { // Only fail hard if we know we can restart eventHandler.setContinue(false); } u.setEventHandler(eventHandler); try { jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr)); } catch (UnmarshalException ue) { if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) { /* Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19] Message: The entity "xxe" was referenced, but not declared. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source) */ log.error(ue.getMessage(), ue); throw ue; } if (is.markSupported()) { // When reading from zip, we use a ByteArrayInputStream, // which does support this. log.info("encountered unexpected content; pre-processing"); eventHandler.setContinue(true); try { Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); is.reset(); JAXBResult result = XmlUtils.prepareJAXBResult(jc); XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result); jaxbElement = (E) XmlUtils.unwrap(result.getResult()); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } else { log.error(ue.getMessage(), ue); log.error(".. and mark not supported"); throw ue; } } } catch (XMLStreamException e1) { log.error(e1.getMessage(), e1); throw new JAXBException(e1); } return jaxbElement; }
From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java
public PNGImage(final InputStream inStream, final PNGDecodeParam inDecodeParam) { final InputStream stream; final PNGDecodeParam decodeParam; if (!inStream.markSupported()) { stream = new BufferedInputStream(inStream); } else {/* w ww .j a v a 2s .c o m*/ stream = inStream; } DataInputStream distream = null; try { distream = new DataInputStream(stream); if (inDecodeParam == null) { decodeParam = new PNGDecodeParam(); } else { decodeParam = inDecodeParam; } this.decodeParam = decodeParam; // Get parameter values this.suppressAlpha = decodeParam.getSuppressAlpha(); this.expandPalette = decodeParam.getExpandPalette(); this.output8BitGray = decodeParam.getOutput8BitGray(); this.expandGrayAlpha = decodeParam.getExpandGrayAlpha(); if (decodeParam.getPerformGammaCorrection()) { this.userExponent = decodeParam.getUserExponent(); this.displayExponent = decodeParam.getDisplayExponent(); this.performGammaCorrection = true; this.output8BitGray = true; } this.generateEncodeParam = decodeParam.getGenerateEncodeParam(); if (this.emitProperties) { this.properties.put("file_type", "PNG v. 1.0"); } try { final long magic = distream.readLong(); if (magic != PNG_SIGNATURE) { final String msg = PropertyUtil.getString("PNGImageDecoder0"); throw new RuntimeException(msg); } } catch (final IOException ioe) { log.error("IOException", ioe); final String msg = PropertyUtil.getString("PNGImageDecoder1"); throw new RuntimeException(msg); } do { try { PNGChunk chunk; final String chunkType = PNGChunk.getChunkType(distream); if (chunkType.equals(PNGChunk.ChunkType.IHDR.name())) { chunk = PNGChunk.readChunk(distream); parse_IHDR_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.PLTE.name())) { chunk = PNGChunk.readChunk(distream); parse_PLTE_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.IDAT.name())) { chunk = PNGChunk.readChunk(distream); this.streamVec.add(new ByteArrayInputStream(chunk.getData())); } else if (chunkType.equals(PNGChunk.ChunkType.IEND.name())) { chunk = PNGChunk.readChunk(distream); parse_IEND_chunk(chunk); break; // fall through to the bottom } else if (chunkType.equals(PNGChunk.ChunkType.bKGD.name())) { chunk = PNGChunk.readChunk(distream); parse_bKGD_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.cHRM.name())) { chunk = PNGChunk.readChunk(distream); parse_cHRM_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.gAMA.name())) { chunk = PNGChunk.readChunk(distream); parse_gAMA_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.hIST.name())) { chunk = PNGChunk.readChunk(distream); parse_hIST_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.iCCP.name())) { chunk = PNGChunk.readChunk(distream); parse_iCCP_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.pHYs.name())) { chunk = PNGChunk.readChunk(distream); parse_pHYs_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.sBIT.name())) { chunk = PNGChunk.readChunk(distream); parse_sBIT_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.sRGB.name())) { chunk = PNGChunk.readChunk(distream); parse_sRGB_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.tEXt.name())) { chunk = PNGChunk.readChunk(distream); parse_tEXt_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.tIME.name())) { chunk = PNGChunk.readChunk(distream); parse_tIME_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.tRNS.name())) { chunk = PNGChunk.readChunk(distream); parse_tRNS_chunk(chunk); } else if (chunkType.equals(PNGChunk.ChunkType.zTXt.name())) { chunk = PNGChunk.readChunk(distream); parse_zTXt_chunk(chunk); } else { chunk = PNGChunk.readChunk(distream); // Output the chunk data in raw form final String type = chunk.getTypeString(); final byte[] data = chunk.getData(); if (this.encodeParam != null) { this.encodeParam.addPrivateChunk(type, data); } if (this.emitProperties) { final String key = "chunk_" + this.chunkIndex++ + ':' + type; this.properties.put(key.toLowerCase(), data); } } } catch (final Exception e) { log.error("Exception", e); final String msg = PropertyUtil.getString("PNGImageDecoder2"); throw new RuntimeException(msg); } } while (true); // Final post-processing if (this.significantBits == null) { this.significantBits = new int[this.inputBands]; for (int i = 0; i < this.inputBands; ++i) { this.significantBits[i] = this.bitDepth; } if (this.emitProperties) { this.properties.put("significant_bits", this.significantBits); } } } finally { IOUtils.closeQuietly(distream); } }
From source file:nl.minbzk.dwr.zoeken.enricher.processor.TikaProcessor.java
/** * Transformt the given input URI into an InputStream. * * @param inputUri/*from w w w.j a va2 s . com*/ * @param file * @return InputStream * @throws Exception */ private InputStream readInputUri(final String inputUri, final File file) throws Exception { InputStream inputStream; if (file.isFile()) { inputStream = new FileInputStream(file); if (logger.isInfoEnabled()) logger.info("Fetching given input file " + inputUri); } else { try { URI uri = new URI(inputUri); if (inputUri.startsWith("data://")) { if (logger.isInfoEnabled()) logger.info("Fetching data URI with ID " + inputUri.substring(7)); inputStream = retrieveGridStream(inputUri.substring(7)); } else { if (logger.isInfoEnabled()) logger.info("Fetching given input URL " + inputUri); inputStream = uri.toURL().openStream(); } } catch (URISyntaxException e) { throw new Exception("The given input URI is neither an existing file nor a valid URL."); } } if (!inputStream.markSupported()) inputStream = new BufferedInputStream(inputStream); return inputStream; }
From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java
public PNGRed(final InputStream inStream, final PNGDecodeParam inDecodeParam) { final InputStream stream; if (!inStream.markSupported()) { stream = new BufferedInputStream(inStream); } else {//from w w w. j a va 2 s .co m stream = inStream; } DataInputStream distream = null; try { distream = new DataInputStream(stream); final PNGDecodeParam decodeParam; if (inDecodeParam == null) { decodeParam = new PNGDecodeParam(); } else { decodeParam = inDecodeParam; } this.decodeParam = decodeParam; // Get parameter values this.suppressAlpha = decodeParam.getSuppressAlpha(); this.expandPalette = decodeParam.getExpandPalette(); this.output8BitGray = decodeParam.getOutput8BitGray(); this.expandGrayAlpha = decodeParam.getExpandGrayAlpha(); if (decodeParam.getPerformGammaCorrection()) { this.userExponent = decodeParam.getUserExponent(); this.displayExponent = decodeParam.getDisplayExponent(); this.performGammaCorrection = true; this.output8BitGray = true; } this.generateEncodeParam = decodeParam.getGenerateEncodeParam(); if (this.emitProperties) { this.properties.put("file_type", "PNG v. 1.0"); } try { final long magic = distream.readLong(); if (magic != 0x89504e470d0a1a0aL) { final String msg = PropertyUtil.getString("PNGImageDecoder0"); throw new RuntimeException(msg); } } catch (final Exception e) { log.error("Exception", e); final String msg = PropertyUtil.getString("PNGImageDecoder1"); throw new RuntimeException(msg); } do { try { PNGChunk chunk; final String chunkType = getChunkType(distream); if (chunkType.equals("IHDR")) { chunk = readChunk(distream); parse_IHDR_chunk(chunk); } else if (chunkType.equals("PLTE")) { chunk = readChunk(distream); parse_PLTE_chunk(chunk); } else if (chunkType.equals("IDAT")) { chunk = readChunk(distream); this.streamVec.add(new ByteArrayInputStream(chunk.getData())); } else if (chunkType.equals("IEND")) { chunk = readChunk(distream); parse_IEND_chunk(chunk); break; // fall through to the bottom } else if (chunkType.equals("bKGD")) { chunk = readChunk(distream); parse_bKGD_chunk(chunk); } else if (chunkType.equals("cHRM")) { chunk = readChunk(distream); parse_cHRM_chunk(chunk); } else if (chunkType.equals("gAMA")) { chunk = readChunk(distream); parse_gAMA_chunk(chunk); } else if (chunkType.equals("hIST")) { chunk = readChunk(distream); parse_hIST_chunk(chunk); } else if (chunkType.equals("iCCP")) { chunk = readChunk(distream); parse_iCCP_chunk(chunk); } else if (chunkType.equals("pHYs")) { chunk = readChunk(distream); parse_pHYs_chunk(chunk); } else if (chunkType.equals("sBIT")) { chunk = readChunk(distream); parse_sBIT_chunk(chunk); } else if (chunkType.equals("sRGB")) { chunk = readChunk(distream); parse_sRGB_chunk(chunk); } else if (chunkType.equals("tEXt")) { chunk = readChunk(distream); parse_tEXt_chunk(chunk); } else if (chunkType.equals("tIME")) { chunk = readChunk(distream); parse_tIME_chunk(chunk); } else if (chunkType.equals("tRNS")) { chunk = readChunk(distream); parse_tRNS_chunk(chunk); } else if (chunkType.equals("zTXt")) { chunk = readChunk(distream); parse_zTXt_chunk(chunk); } else { chunk = readChunk(distream); // Output the chunk data in raw form final String type = chunk.getTypeString(); final byte[] data = chunk.getData(); if (this.encodeParam != null) { this.encodeParam.addPrivateChunk(type, data); } if (this.emitProperties) { final String key = "chunk_" + this.chunkIndex++ + ':' + type; this.properties.put(key.toLowerCase(), data); } } } catch (final Exception e) { log.error("Exception", e); final String msg = PropertyUtil.getString("PNGImageDecoder2"); throw new RuntimeException(msg); } } while (true); // Final post-processing if (this.significantBits == null) { this.significantBits = new int[this.inputBands]; for (int i = 0; i < this.inputBands; ++i) { this.significantBits[i] = this.bitDepth; } if (this.emitProperties) { this.properties.put("significant_bits", this.significantBits); } } } finally { IOUtils.closeQuietly(distream); IOUtils.closeQuietly(stream); } }