List of usage examples for java.io BufferedInputStream mark
public synchronized void mark(int readlimit)
mark
method of InputStream
. From source file:Hash.Hash.java
private static byte[] startOfScanTiff(File file, BufferedInputStream in) throws IOException { in.mark(0); int c;// ww w. j av a 2 s .c o m long j = 0; Boolean endian = null; c = in.read(); if (c == 73) { if (in.read() == 73) endian = true; } else if (c == 77) { if (in.read() == 77) endian = false; } if (endian == null) { return null; } long tiffCheck = readEndianValue(in, 2, endian); if (tiffCheck != 42) { return null; } ifdCursor cursor = new ifdCursor(file, endian, readEndianValue(in, 4, endian)); if (cursor.getPointer() == -1) { return null; } if (cursor.getPointer() == 0) { return null; } return readIFDirectory(cursor, in); }
From source file:com.cyberway.issue.crawler.frontier.RecoveryJournal.java
/** * Read a line from the given bufferedinputstream into the MutableString. * Return true if a line was read; false if EOF. * // w w w .j a va 2 s .co m * @param is * @param read * @return True if we read a line. * @throws IOException */ private static boolean readLine(BufferedInputStream is, MutableString read) throws IOException { read.length(0); int c = is.read(); while ((c != -1) && c != '\n' && c != '\r') { read.append((char) c); c = is.read(); } if (c == -1 && read.length() == 0) { // EOF and none read; return false return false; } if (c == '\n') { // consume LF following CR, if present is.mark(1); if (is.read() != '\r') { is.reset(); } } // a line (possibly blank) was read return true; }
From source file:immf.Util.java
@SuppressWarnings("unchecked") public static InputStream png2gif(InputStream is) throws IOException { InputStream gis = null;/* w w w . j a va2s . c o m*/ BufferedInputStream pis = new BufferedInputStream(is); try { // iPhone iOS??iOS 4.3??????? pis.mark(0); ImageInputStream iis = ImageIO.createImageInputStream(pis); Iterator i = ImageIO.getImageReaders(iis); if (i.hasNext() && ((ImageReader) i.next()).getFormatName().equals("gif")) { // ???gif????????????? pis.reset(); gis = pis; } } catch (Exception e) { } finally { pis.reset(); } if (gis != null) { return gis; } // PNG -> GIF? try { BufferedImage png = ImageIO.read(pis); ByteArrayOutputStream converted = new ByteArrayOutputStream(); if (ImageIO.write(png, "gif", converted)) { gis = new ByteArrayInputStream(converted.toByteArray()); } } catch (Exception e) { } if (gis != null) { return gis; } else { pis.reset(); return pis; } }
From source file:com.zimbra.cs.service.FeedManager.java
private static int getLeadingChar(BufferedInputStream is, StringBuilder charset) throws IOException { is.mark(128); // check for any BOMs that would override the specified charset int ch = is.read(); switch (ch) { case 0xEF:/*from w w w. j a v a2 s . co m*/ if (is.read() == 0xBB && is.read() == 0xBF) { is.mark(128); ch = is.read(); charset.setLength(0); charset.append("utf-8"); } break; case 0xFE: if (is.read() == 0xFF && is.read() == 0x00) { ch = is.read(); charset.setLength(0); charset.append("utf-16"); } break; case 0xFF: if (is.read() == 0xFE) { ch = is.read(); charset.setLength(0); charset.append("utf-16"); } break; } // skip up to 120 bytes of leading whitespace for (int index = 0; index < 120 && (ch == '\0' || Character.isWhitespace(ch)); index++) ch = is.read(); // reset to the original state and return the first non-whtespace character is.reset(); return ch; }
From source file:util.io.IOUtilities.java
/** * This method tries to open an inputstream as gzip and uncompresses it. If it fails, * a normal inputstream is returned// w w w . j a va 2 s . com * * @param is Inputstream that could be compressed * @return uncompressed inputstream * @throws IOException Problems during opening of the Stream * @since 3.0 */ public static InputStream openSaveGZipInputStream(final InputStream is) throws IOException { final BufferedInputStream bis = new BufferedInputStream(is); bis.mark(64); try { final InputStream result = new GZIPInputStream(bis); return result; } catch (final IOException e) { e.printStackTrace(); bis.reset(); return bis; } }
From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java
private static String getXMLGuessEncoding(final BufferedInputStream is) throws IOException { String encoding = null;/*from w ww . j ava 2 s. c o m*/ int[] bytes = new int[4]; is.mark(4); bytes[0] = is.read(); bytes[1] = is.read(); bytes[2] = is.read(); bytes[3] = is.read(); is.reset(); if (bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F) { encoding = UTF_16BE; } else if (bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00) { encoding = UTF_16LE; } else if (bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D) { encoding = UTF_8; } return encoding; }
From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java
private static String getBOMEncoding(final BufferedInputStream is) throws IOException { String encoding = null;//from ww w. j ava2s . co m int[] bytes = new int[3]; is.mark(3); bytes[0] = is.read(); bytes[1] = is.read(); bytes[2] = is.read(); if (bytes[0] == 0xFE && bytes[1] == 0xFF) { encoding = UTF_16BE; is.reset(); is.read(); is.read(); } else if (bytes[0] == 0xFF && bytes[1] == 0xFE) { encoding = UTF_16LE; is.reset(); is.read(); is.read(); } else if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { encoding = UTF_8; } else { is.reset(); } return encoding; }
From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java
/** * Returns the encoding declared in <?xml encoding=...?>, or NULL if none. * //from w ww .ja va 2 s . c om * @param is * @param guessedEnc * @return String * @throws IOException */ private static String getXmlProlog(final BufferedInputStream is, final String guessedEnc) { try { 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; while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) { offset += c; max -= c; try { c = is.read(bytes, offset, max); } catch (IOException e) { if (offset > 0) is.reset(); throw e; } firstGT = new String(bytes, 0, offset).indexOf(">"); } if (firstGT == -1) { if (offset > 0) is.reset(); 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(); Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes, 0, firstGT + 1), guessedEnc); BufferedReader bReader = new BufferedReader(reader); 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; } catch (IOException e) { if (logger.isDebugEnabled()) logger.debug("Could not retrieve the XML prolog from the given input: " + e.getMessage()); return null; } }
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); try {//from ww w. ja v a2 s . c o m 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:com.github.benmanes.caffeine.cache.simulator.parser.AbstractTraceReader.java
/** Returns the input stream, decompressing if required. */ private InputStream readFile(String filePath) throws IOException { BufferedInputStream input = new BufferedInputStream(openFile(filePath), BUFFER_SIZE); input.mark(100); try {//from w w w . j av a 2 s .c o m return new XZInputStream(input); } catch (IOException e) { input.reset(); } try { return new CompressorStreamFactory().createCompressorInputStream(input); } catch (CompressorException e) { input.reset(); } try { return new ArchiveStreamFactory().createArchiveInputStream(input); } catch (ArchiveException e) { input.reset(); } return input; }