List of usage examples for java.io BufferedInputStream reset
public synchronized void reset() throws IOException
reset
method of InputStream
. From source file:com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader.java
/** Returns the input stream, decompressing if required. */ private InputStream readFile(String filePath) throws IOException { BufferedInputStream input = new BufferedInputStream(openFile(filePath)); input.mark(100);/* www.jav a 2 s . c om*/ try { return new CompressorStreamFactory().createCompressorInputStream(input); } catch (CompressorException e) { input.reset(); } try { return new ArchiveStreamFactory().createArchiveInputStream(input); } catch (ArchiveException e) { input.reset(); } return input; }
From source file:org.apache.nifi.csv.CSVReader.java
@Override public RecordReader createRecordReader(final Map<String, String> variables, final InputStream in, final ComponentLog logger) throws IOException, SchemaNotFoundException { // Use Mark/Reset of a BufferedInputStream in case we read from the Input Stream for the header. final BufferedInputStream bufferedIn = new BufferedInputStream(in); bufferedIn.mark(1024 * 1024);//from w ww . j a v a 2 s . com final RecordSchema schema = getSchema(variables, new NonCloseableInputStream(bufferedIn), null); bufferedIn.reset(); return new CSVRecordReader(bufferedIn, logger, schema, csvFormat, firstLineIsHeader, ignoreHeader, dateFormat, timeFormat, timestampFormat, charSet); }
From source file:fedora.server.rest.RestUtil.java
/** * Retrieves the contents of the HTTP Request. * @return InputStream from the request//w w w . j av a 2 s .co m */ public RequestContent getRequestContent(HttpServletRequest request, HttpHeaders headers) throws Exception { RequestContent rContent = null; // See if the request is a multi-part file upload request if (ServletFileUpload.isMultipartContent(request)) { // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request, use the first available File item FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); if (!item.isFormField()) { rContent = new RequestContent(); rContent.contentStream = item.openStream(); rContent.mimeType = item.getContentType(); FileItemHeaders itemHeaders = item.getHeaders(); if (itemHeaders != null) { String contentLength = itemHeaders.getHeader("Content-Length"); if (contentLength != null) { rContent.size = Integer.parseInt(contentLength); } } break; } } } else { // If the content stream was not been found as a multipart, // try to use the stream from the request directly if (rContent == null) { if (request.getContentLength() > 0) { rContent = new RequestContent(); rContent.contentStream = request.getInputStream(); rContent.size = request.getContentLength(); } else { String transferEncoding = request.getHeader("Transfer-Encoding"); if (transferEncoding != null && transferEncoding.contains("chunked")) { BufferedInputStream bis = new BufferedInputStream(request.getInputStream()); bis.mark(2); if (bis.read() > 0) { bis.reset(); rContent = new RequestContent(); rContent.contentStream = bis; } } } } } // Attempt to set the mime type and size if not already set if (rContent != null) { if (rContent.mimeType == null) { MediaType mediaType = headers.getMediaType(); if (mediaType != null) { rContent.mimeType = mediaType.toString(); } } if (rContent.size == 0) { List<String> lengthHeaders = headers.getRequestHeader("Content-Length"); if (lengthHeaders != null && lengthHeaders.size() > 0) { rContent.size = Integer.parseInt(lengthHeaders.get(0)); } } } return rContent; }
From source file:fr.mby.opa.picsimpl.service.BasicPictureFactory.java
protected void loadMetadata(final Picture picture, final BufferedInputStream stream) throws IOException { try {/*from w ww . j av a2s .co m*/ stream.reset(); final Metadata metadata = ImageMetadataReader.readMetadata(stream, false); // Process all metadatas final JSONStringer jsonStringer = new JSONStringer(); // Start main object jsonStringer.object(); final Iterator<Directory> dirIt = metadata.getDirectories().iterator(); while (dirIt.hasNext()) { final Directory dir = dirIt.next(); // Start Directory jsonStringer.key(dir.getName()).object(); final Collection<Tag> dirTags = dir.getTags(); for (final Tag tag : dirTags) { final int tagType = tag.getTagType(); final String tagName = tag.getTagName(); final String tagDesc = tag.getDescription(); // Add Tag jsonStringer.key(tagName + "[" + tagType + "]").value(tagDesc); } // End Directory jsonStringer.endObject(); } // End main object jsonStringer.endObject(); final String jsonMetadata = jsonStringer.toString(); picture.setJsonMetadata(jsonMetadata); // Process specific metadata // Times final Timestamp originalTime = this.findOriginalTime(metadata); picture.setOriginalTime(originalTime); picture.setCreationTime(new Timestamp(System.currentTimeMillis())); } catch (final ImageProcessingException e) { // Unable to process metadata BasicPictureFactory.LOG.warn("Unable to read metadata !", e); } catch (final JSONException e) { // Technical problem BasicPictureFactory.LOG.warn("Problem while converting Metadata to JSON !", e); throw new RuntimeException("Problem while converting Metadata to JSON !", e); } }
From source file:pl.nask.hsn2.service.analysis.JSWekaAnalyzer.java
/** * Returns hex string representation of MD5 hash for given file. * /*from ww w.j av a 2 s.com*/ * @param fileName * @return * @throws IOException */ public final String md5hashFromFile(BufferedInputStream bufferedInputStream) throws IOException { bufferedInputStream.reset(); String result = null; MessageDigest md; try { md = MessageDigest.getInstance("MD5"); md.reset(); try (InputStream dis = new DigestInputStream(new WhiteListFileInputStream(bufferedInputStream), md)) { while (dis.read() != -1) { //NOPMD // Nothing to do. } char[] md5 = Hex.encodeHex(md.digest()); result = String.valueOf(md5); } } catch (NoSuchAlgorithmException e) { LOGGER.error("Could not create MD5 hash for whitelisting.\n{}", e); result = ""; } return result; }
From source file:org.lockss.plugin.usdocspln.gov.gpo.fdsys.GPOFDSysHtmlFilterFactory.java
public InputStream createFilteredInputStream(ArchivalUnit au, InputStream in, String encoding) throws PluginException { BufferedInputStream bis = new BufferedInputStream(in, 2048); bis.mark(2048);/* w ww.jav a 2s. co m*/ try { boolean filter = shouldFilter(bis, encoding); bis.reset(); if (!filter) { return bis; } } catch (IOException ioe) { logger.debug("IOException while inspecting document to skip filtering", ioe); } NodeFilter[] filters = new NodeFilter[] { /* * Broad area filtering */ /* Document header */ // Differences in the presence and order of <meta> tags and spacing of the <title> tag HtmlNodeFilters.tag("head"), /* Scripts, inline style */ HtmlNodeFilters.tag("script"), HtmlNodeFilters.tag("noscript"), HtmlNodeFilters.tag("style"), /* Header */ HtmlNodeFilters.tagWithAttributeRegex("div", "id", "top-menu-one"), HtmlNodeFilters.tagWithAttributeRegex("div", "id", "top-banner-inside"), HtmlNodeFilters.tagWithAttributeRegex("div", "id", "top-menu-two"), /* Left column (various cells of a two-column table layout) */ HtmlNodeFilters.tagWithAttributeRegex("div", "id", "left-menu"), HtmlNodeFilters.tagWithAttributeRegex("div", "id", "page-details-left-mask"), /* Footer */ HtmlNodeFilters.tagWithAttributeRegex("div", "id", "footer"), /* * Main area */ // Seen in the field: "null" in <a href="/fdsys/search/pagedetails.action?null&bread=true">More Information</a> HtmlNodeFilters.tagWithAttributeRegex("span", "id", "breadcrumbs"), // Whitespace differences inside <div id="page-details-form-mask"> HtmlNodeFilters.tagWithAttributeRegex("div", "id", "page-details-form-mask"), /* * Other */ // Variable Struts identifier, sometime in a comment, sometimes not HtmlNodeFilters.tagWithAttributeRegex("input", "type", "hidden"), HtmlNodeFilters.comment(), // "Email a link to this page" link [probably contained in larger block now] HtmlNodeFilters.tagWithAttributeRegex("a", "href", "^search/notificationPage\\.action\\?emailBody="), // Session ID from search results [probably contained in larger block now] HtmlNodeFilters.tagWithAttributeRegex("form", "action", "jsessionid="), // Differs over time in the presence and placement of rel="nofollow" HtmlNodeFilters.tagWithAttributeRegex("a", "href", "^delivery/getpackage\\.action\\?packageId="), }; HtmlTransform xform = new HtmlTransform() { @Override public NodeList transform(NodeList nodeList) throws IOException { try { nodeList.visitAllNodesWith(new NodeVisitor() { @Override public void visitTag(Tag tag) { String tagName = tag.getTagName().toLowerCase(); if ("a".equals(tagName)) { tag.removeAttribute("onclick"); // Javascript calls changed over time } } }); return nodeList; } catch (ParserException pe) { throw new IOException("ParserException inside HtmlTransform", pe); } } }; InputStream prefilteredStream = new HtmlFilterInputStream(bis, // NOTE: this is 'bis', not 'in' encoding, new HtmlCompoundTransform(HtmlNodeFilterTransform.exclude(new OrFilter(filters)), xform)); try { Reader filteredReader = new InputStreamReader(prefilteredStream, encoding); Reader whitespaceReader = new WhiteSpaceFilter(filteredReader); return new ReaderInputStream(whitespaceReader, encoding); } catch (UnsupportedEncodingException uee) { throw new PluginException(uee); } }
From source file:org.callimachusproject.io.CarInputStream.java
private boolean scanForClass(BufferedInputStream in, String type) throws IOException { assert type != null; byte[] peek = new byte[RDFS_PEEK_SIZE]; in.mark(RDFS_PEEK_SIZE);/*from w w w . j a va 2 s .co m*/ int len = IOUtil.readBytes(in, peek); in.reset(); URI uri = new URIImpl("http://example.com/" + entry.getName()); LinkedHashModel model = new LinkedHashModel(); try { RDFParserRegistry registry = org.openrdf.rio.RDFParserRegistry.getInstance(); RDFParser parser = registry.get(registry.getFileFormatForMIMEType(type)).getParser(); parser.setRDFHandler(new StatementCollector(model)); parser.parse(new ByteArrayInputStream(peek, 0, len), uri.toString()); } catch (RDFParseException e) { // ignore } catch (RDFHandlerException e) { // ignore } return model.contains(uri, RDF.TYPE, OWL.ONTOLOGY) || model.contains(uri, RDF.TYPE, OWL.CLASS) || model.contains(uri, RDF.TYPE, OWL.OBJECTPROPERTY) || model.contains(uri, RDF.TYPE, OWL.DATATYPEPROPERTY) || model.contains(uri, RDF.TYPE, OWL.FUNCTIONALPROPERTY) || model.contains(uri, RDF.TYPE, RDFS.CLASS) || model.contains(uri, RDF.TYPE, RDF.PROPERTY); }
From source file:fr.mby.opa.picsimpl.service.BasicPictureFactory.java
protected void loadPicture(final Picture picture, final byte[] contents, final BufferedInputStream stream) throws IOException, UnsupportedPictureTypeException { // Load BufferedImage stream.reset(); final BufferedImage originalImage = ImageIO.read(stream); if (originalImage == null) { throw new UnsupportedPictureTypeException(picture.getFilename()); }/* w ww . j a v a 2s. c o m*/ final int width = originalImage.getWidth(); final int height = originalImage.getHeight(); picture.setWidth(width); picture.setHeight(height); picture.setSize(contents.length); // Load thumbnail this.loadThumbnail(picture, originalImage); // Search for picture format String format = null; // with Java image readers stream.reset(); final ImageInputStream imageStream = ImageIO.createImageInputStream(stream); final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageStream); if (imageReaders.hasNext()) { format = imageReaders.next().getFormatName(); } // In picture filename extension if (format == null) { format = FilenameUtils.getExtension(picture.getFilename()); } // Default format if (format == null) { format = BasicPictureFactory.DEFAULT_PICTURE_FORMAT; } picture.setFormat(format.toLowerCase()); // Build Contents final BinaryImage image = new BinaryImage(); image.setData(contents); image.setFilename(picture.getFilename()); image.setFormat(format); image.setWidth(width); image.setHeight(height); picture.setImage(image); }
From source file:org.eclipse.xtend.expression.ResourceManagerDefaultImpl.java
/** * Creates a Reader for the given InputStream. If no explicit file encoding * is set this method will try to autodetect the file's encoding. * //from ww w.j a va 2 s. c om * @param in * Some resource input stream * @return A Reader for the stream * @since 4.2 */ protected Reader createReader(final InputStream in) { Reader reader = null; if (fileEncoding != null) { try { reader = new InputStreamReader(in, fileEncoding); } catch (final UnsupportedEncodingException e) { log.error("Unsupported encoding falling back to default...", e); reader = new InputStreamReader(in); } } else { Charset encoding = null; // Buffer the original stream since we want to re-read it BufferedInputStream is = new BufferedInputStream(in); try { // Read some bytes from the stream is.mark(65); byte[] buf = new byte[64]; is.read(buf); // reset the stream is.reset(); // Special handling for Xpand files on Mac: Try to detect // the opening Guillemot bracket for MacRoman encoding for (int i = 0; i < buf.length; i++) { if (buf[i] == -57) { // opening Guillemot bracket encoding = Charset.forName("MacRoman"); break; } } // Use com.ibm.icu for autodetection if (encoding == null) { CharsetDetector det = new CharsetDetector(); det.setText(buf); CharsetMatch match = det.detect(); if (match != null) { encoding = Charset.forName(match.getName()); } } // Create the reader with the detected encoding if (encoding != null) { reader = new InputStreamReader(is, encoding); } else { log.warn("Failed autodetecting encoding. Falling back to default..."); reader = new InputStreamReader(is); } } catch (IOException e) { log.warn("Failed autodetecting encoding. Falling back to default...", e); reader = new InputStreamReader(in); } } return reader; }
From source file:org.gradle.api.plugins.buildcomparison.outcome.internal.archive.entry.FileToArchiveEntrySetTransformer.java
private ImmutableSet<ArchiveEntry> walk(InputStream archiveInputStream, ImmutableSet.Builder<ArchiveEntry> allEntries, ImmutableList<String> parentPaths) { ImmutableSet.Builder<ArchiveEntry> entries = ImmutableSet.builder(); ZipInputStream zipStream = new ZipInputStream(archiveInputStream); try {/*from w w w .java 2s .c o m*/ ZipEntry entry = zipStream.getNextEntry(); while (entry != null) { ArchiveEntry.Builder builder = new ArchiveEntry.Builder(); builder.setParentPaths(parentPaths); builder.setPath(entry.getName()); builder.setCrc(entry.getCrc()); builder.setDirectory(entry.isDirectory()); builder.setSize(entry.getSize()); if (!builder.isDirectory() && (zipStream.available() == 1)) { boolean zipEntry; final BufferedInputStream bis = new BufferedInputStream(zipStream) { @Override public void close() throws IOException { } }; bis.mark(Integer.MAX_VALUE); zipEntry = new ZipInputStream(bis).getNextEntry() != null; bis.reset(); if (zipEntry) { ImmutableList<String> nextParentPaths = ImmutableList.<String>builder().addAll(parentPaths) .add(entry.getName()).build(); ImmutableSet<ArchiveEntry> subEntries = walk(bis, allEntries, nextParentPaths); builder.setSubEntries(subEntries); } } ArchiveEntry archiveEntry = builder.build(); entries.add(archiveEntry); allEntries.add(archiveEntry); zipStream.closeEntry(); entry = zipStream.getNextEntry(); } } catch (IOException e) { throw new UncheckedIOException(e); } finally { IOUtils.closeQuietly(zipStream); } return entries.build(); }