List of usage examples for java.io InputStream reset
public synchronized void reset() throws IOException
mark
method was last called on this input stream. From source file:org.apache.xml.security.signature.XMLSignatureInput.java
public void updateOutputStream(OutputStream diOs, boolean c14n11) throws CanonicalizationException, IOException { if (diOs == outputStream) { return;/* w w w.ja va2s. c o m*/ } if (bytes != null) { diOs.write(bytes); return; } else if (_inputOctetStreamProxy == null) { CanonicalizerBase c14nizer = null; if (c14n11) { c14nizer = new Canonicalizer11_OmitComments(); } else { c14nizer = new Canonicalizer20010315OmitComments(); } c14nizer.setWriter(diOs); c14nizer.engineCanonicalize(this); return; } else { InputStream is = getResetableInputStream(); if (bytes != null) { //already read write it, can be rea. diOs.write(bytes, 0, bytes.length); return; } is.reset(); int num; byte[] bytesT = new byte[1024]; while ((num = is.read(bytesT)) > 0) { diOs.write(bytesT, 0, num); } } }
From source file:org.apache.tika.parser.html.charsetdetector.StandardHtmlEncodingDetector.java
@Override public Charset detect(InputStream input, Metadata metadata) throws IOException { int limit = getMarkLimit(); input.mark(limit);// w ww. ja v a2 s . c o m // Never read more than the first META_TAG_BUFFER_SIZE bytes InputStream limitedStream = new BoundedInputStream(input, limit); PreScanner preScanner = new PreScanner(limitedStream); // The order of priority for detection is: // 1. Byte Order Mark Charset detectedCharset = preScanner.detectBOM(); // 2. Transport-level information (Content-Type HTTP header) if (detectedCharset == null) detectedCharset = charsetFromContentType(metadata); // 3. HTML <meta> tag if (detectedCharset == null) detectedCharset = preScanner.scan(); input.reset(); return detectedCharset; }
From source file:com.soma.daemin.fragment.NewPicUploadTaskFragment.java
public Bitmap decodeSampledBitmapFromPath(String filePath, int reqWidth, int reqHeight) throws IOException { File file = new File(filePath); InputStream stream = new BufferedInputStream(new FileInputStream(file)); /*InputStream stream = new BufferedInputStream( mApplicationContext.getContentResolver().openInputStream(fileUri));*/ stream.mark(stream.available());// ww w . ja va 2 s. c o m BitmapFactory.Options options = new BitmapFactory.Options(); // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeStream(stream, null, options); stream.reset(); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; BitmapFactory.decodeStream(stream, null, options); // Decode bitmap with inSampleSize set stream.reset(); return BitmapFactory.decodeStream(stream, null, options); }
From source file:com.google.ratel.deps.io.input.XmlStreamReader.java
/** * Returns the encoding declared in the <?xml encoding=...?>, NULL if none. * * @param is InputStream to create the reader from. * @param guessedEnc guessed encoding// ww w . ja v a 2s. c o m * @return the encoding declared in the <?xml encoding=...?> * @throws IOException thrown if there is a problem reading the stream. */ private static String getXmlProlog(InputStream is, String guessedEnc) throws IOException { String encoding = null; if (guessedEnc != null) { byte[] bytes = new byte[BUFFER_SIZE]; is.mark(BUFFER_SIZE); int offset = 0; int max = BUFFER_SIZE; int c = is.read(bytes, offset, max); int firstGT = -1; String xmlProlog = null; while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) { offset += c; max -= c; c = is.read(bytes, offset, max); xmlProlog = new String(bytes, 0, offset, guessedEnc); firstGT = xmlProlog.indexOf('>'); } if (firstGT == -1) { if (c == -1) { throw new IOException("Unexpected end of XML stream"); } else { throw new IOException("XML prolog or ROOT element not found on first " + offset + " bytes"); } } int bytesRead = offset; if (bytesRead > 0) { is.reset(); BufferedReader bReader = new BufferedReader(new StringReader(xmlProlog.substring(0, firstGT + 1))); StringBuffer prolog = new StringBuffer(); String line = bReader.readLine(); while (line != null) { prolog.append(line); line = bReader.readLine(); } Matcher m = ENCODING_PATTERN.matcher(prolog); if (m.find()) { encoding = m.group(1).toUpperCase(); encoding = encoding.substring(1, encoding.length() - 1); } } } return encoding; }
From source file:org.onosproject.common.app.ApplicationArchive.java
/** * Loads the application descriptor from the specified application archive * stream and saves the stream in the appropriate application archive * directory./* w w w . j a va 2 s . c o m*/ * * @param stream application archive stream * @return application descriptor * @throws org.onosproject.app.ApplicationException if unable to read the * archive stream or store * the application archive */ public synchronized ApplicationDescription saveApplication(InputStream stream) { try (InputStream ais = stream) { byte[] cache = toByteArray(ais); InputStream bis = new ByteArrayInputStream(cache); boolean plainXml = isPlainXml(cache); ApplicationDescription desc = plainXml ? parsePlainAppDescription(bis) : parseZippedAppDescription(bis); checkState(!appFile(desc.name(), APP_XML).exists(), "Application %s already installed", desc.name()); if (plainXml) { expandPlainApplication(cache, desc); } else { bis.reset(); expandZippedApplication(bis, desc); bis.reset(); saveApplication(bis, desc); } installArtifacts(desc); return desc; } catch (IOException e) { throw new ApplicationException("Unable to save application", e); } }
From source file:org.apache.tika.parser.pkg.PackageExtractor.java
public void parse(InputStream stream) throws IOException, SAXException, TikaException { XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument();//from ww w .j a v a 2 s. c o m // At the end we want to close the package/compression stream to // release any associated resources, but the underlying document // stream should not be closed stream = new CloseShieldInputStream(stream); // Capture two bytes to determine the packaging/compression format if (!stream.markSupported()) { stream = new BufferedInputStream(stream); } stream.mark(2); int a = stream.read(); int b = stream.read(); stream.reset(); // Select decompression or unpacking mechanism based on the two bytes if (a == 'B' && b == 'Z') { metadata.set(Metadata.CONTENT_TYPE, "application/x-bzip"); decompress(new BZip2CompressorInputStream(stream), xhtml); } else if (a == 0x1f && b == 0x8b) { metadata.set(Metadata.CONTENT_TYPE, "application/x-gzip"); decompress(new GZIPInputStream(stream), xhtml); } else if (a == 'P' && b == 'K') { metadata.set(Metadata.CONTENT_TYPE, "application/zip"); unpack(new ZipArchiveInputStream(stream), xhtml); } else if ((a == '0' && b == '7') || (a == 0x71 && b == 0xc7) || (a == 0xc7 && b == 0x71)) { metadata.set(Metadata.CONTENT_TYPE, "application/x-cpio"); unpack(new CpioArchiveInputStream(stream), xhtml); } else if (a == '=' && (b == '<' || b == '!')) { metadata.set(Metadata.CONTENT_TYPE, "application/x-archive"); unpack(new ArArchiveInputStream(stream), xhtml); } else { metadata.set(Metadata.CONTENT_TYPE, "application/x-tar"); unpack(new TarArchiveInputStream(stream), xhtml); } xhtml.endDocument(); }
From source file:com.graphhopper.reader.OSMInputFile.java
@SuppressWarnings("unchecked") private InputStream decode(File file) throws IOException { final String name = file.getName(); InputStream ips = null; try {//from w ww . ja va2s . co m ips = new BufferedInputStream(new FileInputStream(file), 50000); } catch (FileNotFoundException e) { throw new RuntimeException(e); } ips.mark(10); // check file header byte header[] = new byte[6]; ips.read(header); /* can parse bz2 directly with additional lib if (header[0] == 'B' && header[1] == 'Z') { return new CBZip2InputStream(ips); } */ if (header[0] == 31 && header[1] == -117) { ips.reset(); return new GZIPInputStream(ips, 50000); } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9 && (header[3] == 13 || header[3] == 14)) { ips.reset(); binary = true; return ips; } else if (header[0] == 'P' && header[1] == 'K') { ips.reset(); ZipInputStream zip = new ZipInputStream(ips); zip.getNextEntry(); return zip; } else if (name.endsWith(".osm") || name.endsWith(".xml")) { ips.reset(); return ips; } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) { String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream"; try { Class clazz = Class.forName(clName); ips.reset(); Constructor<InputStream> ctor = clazz.getConstructor(InputStream.class, boolean.class); return ctor.newInstance(ips, true); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate " + clName, e); } } else { throw new IllegalArgumentException("Input file is not of valid type " + file.getPath()); } }
From source file:com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin.java
/** * @param filecontent the content of the file * @return true if the file is in zip format (.zip, .jar etc) or false otherwise */// w ww. j av a 2 s . c o m protected boolean isZipFile(InputStream filecontent) { int standardZipHeader = 0x504b0304; filecontent.mark(8); try { DataInputStream datastream = new DataInputStream(filecontent); int fileHeader = datastream.readInt(); return (standardZipHeader == fileHeader); } catch (IOException e) { // The file doesn't have 4 bytes, so it isn't a zip file } finally { // Reset the input stream to the beginning. This may be needed for further reading the archive. try { filecontent.reset(); } catch (IOException e) { e.printStackTrace(); } } return false; }
From source file:org.pentaho.mondrian.publish.PublishToServerCommand.java
/** * helper method to calculate the domain id from the file name, or pass catalog * @param dataInputStream schema file input stream * @param fileName name of schema file on filesystem * @return Look up name from XML otherwise use file name *///from ww w.j a v a 2s. com private String determineDomainCatalogName(InputStream dataInputStream, String fileName) { String domainId = ""; final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(dataInputStream); NodeList schemas = document.getElementsByTagName("Schema"); Node schema = schemas.item(0); Node name = schema.getAttributes().getNamedItem("name"); domainId = name.getTextContent(); dataInputStream.reset(); } catch (Exception e) { LOG.fine("Problem occurred when trying to get schema name from document. Using filename instead."); } if ("".equals(domainId)) { domainId = fileName; } return domainId; }
From source file:io.sledge.core.impl.installer.SledgePackageConfigurer.java
@Override public InputStream configure(InputStream packageStream, String packageName, Properties props) { InputStream resultStream = packageStream; try {/*from w ww. j a v a 2 s . com*/ Path sledgeTmpDir = createSledgeTmpDirectoryIfNotExists(); deleteZipFile(sledgeTmpDir, packageName); Path configurationZipPackagePath = sledgeTmpDir.resolve(packageName); createNewZipfileWithReplacedPlaceholders(packageStream, configurationZipPackagePath, props); resultStream = new FileInputStream(configurationZipPackagePath.toFile()); } catch (IOException e) { log.error("Could not configure package: " + packageName, e); } finally { try { packageStream.reset(); packageStream.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return resultStream; }