Java BufferedReader Create getReader(InputStream is)

Here you can find the source of getReader(InputStream is)

Description

get Reader

License

Open Source License

Declaration

private static Reader getReader(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Main {
    private final static int LOOKAHEAD = 1024;

    private static Reader getReader(InputStream is) throws IOException {
        Reader reader = new BufferedReader(new InputStreamReader(is));
        char c[] = "<?".toCharArray();
        int pos = 0;
        reader.mark(LOOKAHEAD);//from   w  w  w. j  a va  2s.c o  m
        while (true) {
            int value = reader.read();

            // Check to see if we hit the end of the stream.
            if (value == -1) {
                throw new IOException("Encounter end of stream before start of XML.");
            } else if (value == c[pos]) {
                pos++;
            } else {
                if (pos > 0) {
                    pos = 0;
                }
                reader.mark(LOOKAHEAD);
            }

            if (pos == c.length) {
                // We found the character set we were looking for.
                reader.reset();
                break;
            }
        }

        return reader;
    }
}

Related

  1. getReader(final String fileName)
  2. getReader(final String fileName)
  3. getReader(InputStream in)
  4. getReader(InputStream input)
  5. getReader(InputStream is)
  6. getReader(String fileDir, String fileName, String encoding)
  7. getReader(String filename)
  8. getReader(String filename)
  9. getReader(String filename)