Example usage for java.io InputStream markSupported

List of usage examples for java.io InputStream markSupported

Introduction

In this page you can find the example usage for java.io InputStream markSupported.

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    // new input stream created
    InputStream is = new FileInputStream("C://test.txt");

    // returns true if the mark()/reset() supported.
    boolean bool = is.markSupported();
    is.close();//  w  w w.  java2 s.c  om
    System.out.print(bool);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("C://test.txt");

    // read and print characters one by one
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    // mark is set on the input stream
    is.mark(0);//from  www  .  j ava2 s . c  om

    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    if (is.markSupported()) {
        // reset invoked if mark() is supported
        is.reset();
        System.out.println("Char : " + (char) is.read());
        System.out.println("Char : " + (char) is.read());
    }
    is.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream is = new FileInputStream("C://test.txt");

    System.out.println("Characters printed:");
    // create new buffered reader

    // reads and prints BufferedReader
    System.out.println((char) is.read());
    System.out.println((char) is.read());

    // mark invoked at this position
    is.mark(0);//from  w ww .ja  v a  2 s  . c o  m
    System.out.println("mark() invoked");
    System.out.println((char) is.read());
    System.out.println((char) is.read());

    // reset() repositioned the stream to the mark
    if (is.markSupported()) {
        is.reset();
        System.out.println("reset() invoked");
        System.out.println((char) is.read());
        System.out.println((char) is.read());
    } else {
        System.out.print("InputStream does not support reset()");
    }
    is.close();
}

From source file:Main.java

static public final boolean isGzipStm(InputStream in) throws IOException {
    boolean ms = in.markSupported();
    if (ms)//from   w  w w.  j  a v a  2 s. c o  m
        in.mark(10);
    int b1 = in.read();
    int b2 = in.read();
    if (ms)
        in.reset();
    return ((b2 << 8 | b1) == GZIPInputStream.GZIP_MAGIC);
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary./*  w  w  w.  j a  v  a2 s  .c  o  m*/
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}

From source file:net.ovres.util.UncompressedInputStream.java

/**
 * Returns a decompressed version of the given input stream.
 * This is done for any set //from  w  w w .  j a  v  a  2s .  c  o  m
 *
 * @param  raw  the raw input stream
 * @return  the decompressed version of <tt>raw</tt>
 */
public static InputStream convertStream(InputStream raw) throws IOException {
    if (!raw.markSupported()) {
        raw = new BufferedInputStream(raw);
    }

    try {
        return new CompressorStreamFactory().createCompressorInputStream(raw);
    } catch (CompressorException e) {
        // Assume this is because of an unknown compression type.
        // That's ok, it might not be compressed.
        return raw;
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java

/**
 * check if the {@link InputStream} provided is a zip file
 * //  w w w.j av a  2 s.c  o m
 * @param in the stream.
 * @return if it is a ZIP file.
 */
public static boolean isZipStream(InputStream in) {
    if (!in.markSupported()) {
        in = new BufferedInputStream(in);
    }
    boolean isZip = true;
    try {
        in.mark(MAGIC.length);
        for (byte element : MAGIC) {
            if (element != (byte) in.read()) {
                isZip = false;
                break;
            }
        }
        in.reset();
    } catch (IOException e) {
        isZip = false;
    }
    return isZip;
}

From source file:Main.java

/**
 * WARNING: if the <param>inStream</param> instance does not support mark/reset, when this method returns,
 * subsequent reads on <param>inStream</param> will be 256 bytes into the stream. This may not be the expected
 * behavior. FileInputStreams are an example of an InputStream that does not support mark/reset. InputStreams
 * that do support mark/reset will be reset to the beginning of the stream when this method returns.
 * /*w  ww.j  av  a  2  s.com*/
 * @param inStream
 * @return
 * @throws IOException
 */
public static String readEncodingProcessingInstruction(final InputStream inStream) throws IOException {
    final int BUFF_SZ = 256;
    if (inStream.markSupported()) {
        inStream.mark(BUFF_SZ + 1); // BUFF_SZ+1 forces mark to NOT be forgotten
    }
    byte[] buf = new byte[BUFF_SZ];
    int totalBytesRead = 0;
    int bytesRead;
    do {
        bytesRead = inStream.read(buf, totalBytesRead, BUFF_SZ - totalBytesRead);
        if (bytesRead == -1) {
            break;
        }
        totalBytesRead += bytesRead;
    } while (totalBytesRead < BUFF_SZ);

    if (inStream.markSupported()) {
        inStream.reset();
    }

    return new String(buf);
}

From source file:Main.java

/**
 * Detect the XML encoding of the document
 * //from  w ww .j  a  v a2  s  .  co  m
 * @param is The input stream
 * @return The encoding
 * @throws IOException
 */
public static String getEncoding(InputStream is) throws IOException {
    if (!is.markSupported())
        is = new BufferedInputStream(is);

    byte[] buffer = readBuffer(is);
    return getXMLEncoding(buffer);
}

From source file:org.apache.xmlgraphics.xmp.XMPPacketParser.java

/**
 * Locates an XMP packet in a stream, parses it and returns the XMP metadata. If no
 * XMP packet is found until the stream ends, null is returned. Note: This method
 * only finds the first XMP packet in a stream. And it cannot determine whether it
 * has found the right XMP packet if there are multiple packets.
 * @param in the InputStream to search//from ww  w  .  jav a  2 s. co m
 * @return the parsed XMP metadata or null if no XMP packet is found
 * @throws IOException if an I/O error occurs
 * @throws TransformerException if an error occurs while parsing the XMP packet
 */
public static Metadata parse(InputStream in) throws IOException, TransformerException {
    if (!in.markSupported()) {
        in = new java.io.BufferedInputStream(in);
    }
    boolean foundXMP = skipAfter(in, PACKET_HEADER);
    if (!foundXMP) {
        return null;
    }
    //TODO Inspect "begin" attribute!
    if (!skipAfter(in, PACKET_HEADER_END)) {
        throw new IOException("Invalid XMP packet header!");
    }
    //TODO think about not buffering this but for example, parse in another thread
    //ex. using PipedInput/OutputStream
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    //TODO Do with TeeInputStream when Commons IO 1.4 is available
    if (!skipAfter(in, PACKET_TRAILER, baout)) {
        throw new IOException("XMP packet not properly terminated!");
    }

    Metadata metadata = XMPParser.parseXMP(new StreamSource(new ByteArrayInputStream(baout.toByteArray())));
    return metadata;
}