Example usage for java.io BufferedInputStream reset

List of usage examples for java.io BufferedInputStream reset

Introduction

In this page you can find the example usage for java.io BufferedInputStream reset.

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

See the general contract of the reset method of InputStream.

Usage

From source file:org.docx4j.openpackaging.packages.OpcPackage.java

/**
 * Convenience method to create a WordprocessingMLPackage
 * or PresentationMLPackage/*from   w  ww  . j a v  a 2s .c  o m*/
 * from an inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml).
 * It detects the convenient format inspecting two first bytes of stream (magic bytes). 
 * For office 2007 'x' formats, these two bytes are 'PK' (same as zip file)  
  *
 * @param inputStream
 *            The docx file 
 * @param password
 *            The password, if the file is password protected (compound)
 *            
 * @Since 2.8.0           
 */
public static OpcPackage load(final InputStream inputStream, String password) throws Docx4JException {
    //try to detect the type of file using a bufferedinputstream
    final BufferedInputStream bis = new BufferedInputStream(inputStream);
    bis.mark(0);
    final byte[] firstTwobytes = new byte[2];
    int read = 0;
    try {
        read = bis.read(firstTwobytes);
        bis.reset();
    } catch (final IOException e) {
        throw new Docx4JException("Error reading from the stream", e);
    }
    if (read != 2) {
        throw new Docx4JException("Error reading from the stream (no bytes available)");
    }
    if (firstTwobytes[0] == 'P' && firstTwobytes[1] == 'K') { // 50 4B
        return OpcPackage.load(bis, Filetype.ZippedPackage, null);
    } else if (firstTwobytes[0] == (byte) 0xD0 && firstTwobytes[1] == (byte) 0xCF) {
        // password protected docx is a compound file, with signature D0 CF 11 E0 A1 B1 1A E1
        log.info("Detected compound file");
        return OpcPackage.load(bis, Filetype.Compound, password);
    } else {
        //Assume..
        log.info("Assuming Flat OPC XML");
        return OpcPackage.load(bis, Filetype.FlatOPC, null);
    }
}

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. 
 * //from  w  w w .j  ava 2 s .  c  om
 * @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:Main.java

public static String getCharset(File file) {
    String charset = "GBK";
    byte[] first3Bytes = new byte[3];
    try {/*from  ww w  .  j  av a  2 s.c  o m*/
        boolean checked = false;
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        bis.mark(0);
        int read = bis.read(first3Bytes, 0, 3);
        if (read == -1)
            return charset;
        if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
            charset = "UTF-16LE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
            charset = "UTF-16BE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
                && first3Bytes[2] == (byte) 0xBF) {
            charset = "UTF-8";
            checked = true;
        }
        bis.reset();
        if (!checked) {
            int loc = 0;
            while ((read = bis.read()) != -1) {
                loc++;
                if (read >= 0xF0)
                    break;
                if (0x80 <= read && read <= 0xBF)
                    break;
                if (0xC0 <= read && read <= 0xDF) {
                    read = bis.read();
                    if (0x80 <= read && read <= 0xBF)
                        continue;
                    else
                        break;
                } else if (0xE0 <= read && read <= 0xEF) {
                    read = bis.read();
                    if (0x80 <= read && read <= 0xBF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            charset = "UTF-8";
                            break;
                        } else
                            break;
                    } else
                        break;
                }
            }
        }
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return charset;
}

From source file:cn.guoyukun.spring.utils.FileCharset.java

public static String getCharset(final BufferedInputStream is) {
    String charset = DEFAULT_CHARSET;
    byte[] first3Bytes = new byte[3];
    try {//from   www  . jav a2  s.  c om
        boolean checked = false;
        is.mark(0);
        int read = is.read(first3Bytes, 0, 3);
        if (read == -1)
            return charset;
        if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
            charset = "UTF-16LE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
            charset = "UTF-16BE";
            checked = true;
        } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
                && first3Bytes[2] == (byte) 0xBF) {
            charset = "UTF-8";
            checked = true;
        }
        is.reset();
        if (!checked) {
            int loc = 0;

            while ((read = is.read()) != -1 && loc < 100) {
                loc++;
                if (read >= 0xF0)
                    break;
                if (0x80 <= read && read <= 0xBF) // ?BFGBK
                    break;
                if (0xC0 <= read && read <= 0xDF) {
                    read = is.read();
                    if (0x80 <= read && read <= 0xBF) // ? (0xC0 - 0xDF)
                        // (0x80
                        // - 0xBF),?GB?
                        continue;
                    else
                        break;
                } else if (0xE0 <= read && read <= 0xEF) {// ??
                    read = is.read();
                    if (0x80 <= read && read <= 0xBF) {
                        read = is.read();
                        if (0x80 <= read && read <= 0xBF) {
                            charset = "UTF-8";
                            break;
                        } else
                            break;
                    } else
                        break;
                }
            }
        }
        is.reset();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return charset;
}

From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java

private static String getXMLGuessEncoding(final BufferedInputStream is) throws IOException {
    String encoding = null;//from   w  w w . 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:util.io.IOUtilities.java

/**
 * This method tries to open an inputstream as gzip and uncompresses it. If it fails,
 * a normal inputstream is returned//from w w w.j a  va  2 s . c om
 *
 * @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 getBOMEncoding(final BufferedInputStream is) throws IOException {
    String encoding = null;/*from   w  w  w .j av  a 2  s.  c o  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:immf.Util.java

@SuppressWarnings("unchecked")
public static InputStream png2gif(InputStream is) throws IOException {
    InputStream gis = null;//from  w w w . ja v a 2s . co  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.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);/*from   www . jav a2s .  c om*/
    try {
        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;
}

From source file:org.callimachusproject.io.CarInputStream.java

private String detectRdfType(BufferedInputStream in) throws IOException {
    byte[] peek = new byte[200];
    in.mark(200);//from   w w w  .j a v a 2s.c  o m
    int len = IOUtil.readBytes(in, peek);
    in.reset();
    int first = new TextReader(new ByteArrayInputStream(peek, 0, len)).read();
    if (first == '<')
        return "application/rdf+xml";
    return "text/turtle";
}