Example usage for java.io InputStream mark

List of usage examples for java.io InputStream mark

Introduction

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

Prototype

public synchronized void mark(int readlimit) 

Source Link

Document

Marks the current position in this input stream.

Usage

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);

    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();//from w ww .  j a v a  2s. c o m
        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);
    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();/*from  ww w  .j a v a  2s .c  o m*/
        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:org.infoscoop.request.filter.XMLFilter.java

/**
 * @param is/*w w  w.  j  a  va  2 s. co  m*/
 * @return skippedCount
 * @throws IOException
 */
public static int skipEmptyLine(InputStream is) throws IOException {
    is.mark(1);
    int count = 0;
    for (int temp = 0; (temp = is.read()) != -1; count++) {
        char tempCh = (char) temp;
        if (tempCh == '<') {
            break;
        } else {
            is.mark(1);
        }
    }
    is.reset();
    return count;
}

From source file:Main.java

static public final boolean isGzipStm(InputStream in) throws IOException {
    boolean ms = in.markSupported();
    if (ms)/*from   w  ww.  j a  va2 s  . co  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

public static String determineEncoding(InputStream stream) throws IOException {
    stream.mark(20000);
    try {//from  www  . j a v a2s .co m
        int b0 = stream.read();
        int b1 = stream.read();
        int b2 = stream.read();
        int b3 = stream.read();

        if (b0 == 0xFE && b1 == 0xFF)
            return "UTF-16BE";
        else if (b0 == 0xFF && b1 == 0xFE)
            return "UTF-16LE";
        else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF)
            return "UTF-8";
        else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
            return "UTF-16BE";
        else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
            return "UTF-16LE";
        else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
            //            UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding 
            //            which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding 
            //            declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns 
            //            for the relevant ASCII characters, the encoding declaration itself may be read reliably
            InputStreamReader rdr = new InputStreamReader(stream, "US-ASCII");
            String hdr = readFirstLine(rdr);
            return extractEncoding(hdr);
        } else
            return null;
    } finally {
        stream.reset();
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
        throws IOException {
    is.mark(is.available());

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww w.jav a  2s  .c  om*/
    BitmapFactory.decodeStream(is, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    is.reset();

    return BitmapFactory.decodeStream(is, null, options);
}

From source file:Main.java

/** Gets Bitmap from inputStream downsampled.
 *
 * @param bis/*from  w w  w.  j av  a 2  s  .c o m*/
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static Bitmap decodeSampledBitmapFromStream(InputStream bis, int reqWidth, int reqHeight) {

    InputStream is = new BufferedInputStream(bis);

    try {
        is.mark(is.available());
    } catch (IOException e) {
        e.printStackTrace();
    }

    // First decode with inJustDecodeBounds=true to check dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bmp0 = BitmapFactory.decodeStream(is, null, options);

    try {
        is.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Calculate inSampleSize
    //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
    return bmp;
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary.//www .  ja v  a 2  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: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.
 * //www  .ja  va  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

protected static byte[] readBuffer(InputStream is) throws IOException {
    if (is.available() == 0) {
        return new byte[0];
    }/*from  www  .  jav  a2 s  .com*/

    byte[] buffer = new byte[BUFFER_SIZE];
    is.mark(BUFFER_SIZE);
    int bytesRead = is.read(buffer, 0, BUFFER_SIZE);
    int totalBytesRead = bytesRead;

    while (bytesRead != -1 && (totalBytesRead < BUFFER_SIZE)) {
        bytesRead = is.read(buffer, totalBytesRead, BUFFER_SIZE - totalBytesRead);

        if (bytesRead != -1)
            totalBytesRead += bytesRead;
    }

    if (totalBytesRead < BUFFER_SIZE) {
        byte[] smallerBuffer = new byte[totalBytesRead];
        System.arraycopy(buffer, 0, smallerBuffer, 0, totalBytesRead);
        smallerBuffer = buffer;
    }

    is.reset();
    return buffer;
}