Example usage for java.io BufferedReader skip

List of usage examples for java.io BufferedReader skip

Introduction

In this page you can find the example usage for java.io BufferedReader skip.

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skips characters.

Usage

From source file:com.github.jarlakxen.embedphantomjs.executor.PhantomJSConsoleExecutor.java

private String readPhantomJSOutput(InputStream processInput, List<String> endLines) throws IOException {

    final StringBuilder out = new StringBuilder();

    BufferedReader in = new BufferedReader(new InputStreamReader(processInput, "UTF-8"));

    while (true) {
        String line = in.readLine();

        if (line.equals(PHANTOMJS_PARSER_ERROR_PREFIX)) {
            return line;
        }//  w ww .j  ava2s . c  o  m

        if (line == null || endLines.contains(line)) {
            if (StringUtils.isNotBlank(consolePrefix)) {
                in.skip(consolePrefix.length());
            }
            break;
        }

        if (out.length() > 0) {
            out.append("\n");
        }

        out.append(line);
    }

    return out.toString();
}

From source file:org.esa.nest.dataio.dem.EarthGravitationalModel96.java

private EarthGravitationalModel96() {

    // get absolute file path
    final String filePath = Settings.instance().get("AuxData.egm96AuxDataPath");
    final String fileName = filePath + File.separator + NAME;

    // get reader
    FileInputStream stream;/*from  w w  w .  j  a va  2 s  .c o  m*/
    try {
        stream = new FileInputStream(fileName);
    } catch (FileNotFoundException e) {
        throw new OperatorException("File not found: " + fileName);
    }

    final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

    // read data from file and save them in 2-D array
    String line = "";
    StringTokenizer st;
    int rowIdx = 0;
    int colIdx = 0;
    try {

        final int numLatLinesToSkip = 0;
        final int numCharInHeader = NUM_CHAR_PER_NORMAL_LINE + NUM_CHAR_PER_EMPTY_LINE;
        final int numCharInEachLatLine = NUM_OF_BLOCKS_PER_LAT * BLOCK_HEIGHT * NUM_CHAR_PER_NORMAL_LINE
                + (NUM_OF_BLOCKS_PER_LAT + 1) * NUM_CHAR_PER_EMPTY_LINE + NUM_CHAR_PER_SHORT_LINE;

        final int totalCharToSkip = numCharInHeader + numCharInEachLatLine * numLatLinesToSkip;
        reader.skip(totalCharToSkip);

        // get the lat lines from 90 deg to -90 deg 45 min
        final int numLinesInEachLatLine = NUM_OF_BLOCKS_PER_LAT * (BLOCK_HEIGHT + 1) + 2;
        final int numLinesToRead = NUM_LATS * numLinesInEachLatLine;
        int linesRead = 0;
        for (int i = 0; i < numLinesToRead - 1; i++) { // -1 because the last line read from file is null

            line = reader.readLine();
            linesRead++;
            if (!line.equals("")) {
                st = new StringTokenizer(line);
                final int numCols = st.countTokens();
                for (int j = 0; j < numCols; j++) {
                    egm[rowIdx][colIdx] = Float.parseFloat(st.nextToken());
                    colIdx++;
                }
            }

            if (linesRead % numLinesInEachLatLine == 0) {
                rowIdx++;
                colIdx = 0;
            }
        }

        reader.close();
        stream.close();

    } catch (IOException e) {
        throw new OperatorException(e);
    }
}

From source file:org.karndo.piracy.Scraper.java

/**
 * /*from www  . ja  va2  s.com*/
 * @param url
 * @return 
 */
private String get_piracy_data(String url) throws IOException {
    InputStream instream = null;
    BufferedReader reader = null;

    //Some Apache HTTP objects for accessing the website
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        instream = entity.getContent();
    }

    //read characters from the instream
    reader = new BufferedReader(new InputStreamReader(instream));
    String line = "";
    String data = "";
    int count = 0;
    while ((line = reader.readLine()) != null) {
        //find the right line
        if (line.contains(
                "http://www.icc-ccs.org/plugins/fabrik_visuali" + "zation/googlemap/googlemap-min.js")) {
            //TODO discover why it doesn't read another line without
            //the reader skipping
            reader.skip(1);
            data = (reader.readLine());
        }
    }

    instream.close();
    return data;

}

From source file:gate.corpora.DocumentContentImpl.java

/** Contruction from URL and offsets. */
public DocumentContentImpl(URL u, String encoding, Long start, Long end) throws IOException {

    int readLength = 0;
    char[] readBuffer = new char[INTERNAL_BUFFER_SIZE];

    BufferedReader uReader = null;
    InputStream uStream = null;/*from   w w w  .ja  v a2s.  com*/
    StringBuffer buf = new StringBuffer();

    long s = 0, e = Long.MAX_VALUE;
    if (start != null && end != null) {
        s = start.longValue();
        e = end.longValue();
    }

    try {
        URLConnection conn = u.openConnection();
        uStream = conn.getInputStream();

        if ("gzip".equals(conn.getContentEncoding())) {
            uStream = new GZIPInputStream(uStream);
        }

        if (encoding != null && !encoding.equalsIgnoreCase("")) {
            uReader = new BomStrippingInputStreamReader(uStream, encoding, INTERNAL_BUFFER_SIZE);
        } else {
            uReader = new BomStrippingInputStreamReader(uStream, INTERNAL_BUFFER_SIZE);
        }
        ;

        // 1. skip S characters
        uReader.skip(s);

        // 2. how many character shall I read?
        long toRead = e - s;

        // 3. read gtom source into buffer
        while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) {
            if (toRead < readLength) {
                //well, if toRead(long) is less than readLenght(int)
                //then there can be no overflow, so the cast is safe
                readLength = (int) toRead;
            }

            buf.append(readBuffer, 0, readLength);
            toRead -= readLength;
        }
    } finally {
        // 4.close reader
        IOUtils.closeQuietly(uReader);
        IOUtils.closeQuietly(uStream);
    }

    content = new String(buf);
    originalContent = content;
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.por.PORFileReader.java

private void decodeSec2(BufferedReader reader) throws IOException {
    dbgLog.fine("***** decodeSec2(): start *****");
    if (reader == null) {
        throw new IllegalArgumentException("decodeSec2: stream == null!");
    }/*  w ww. ja va  2  s .  com*/

    // Because a 64-bit machine may not save the first 40
    // bytes of a POR file in a way as a 32-bit machine does,
    // the first 5 lines of a POR file is excluded from the read-back
    // file and the new 1st line contains the format mark "SPSSPORT"
    // somewhere in it.

    // mark the start position for the later rewind
    if (reader.markSupported()) {
        reader.mark(100000);
    }

    char[] sixthLineCharArray = new char[80];
    int nbytes_sixthLine = reader.read(sixthLineCharArray);

    String sixthLine = new String(sixthLineCharArray);
    dbgLog.info("sixthLineCharArray=" + Arrays.deepToString(ArrayUtils.toObject(sixthLineCharArray)));
    int signatureLocation = sixthLine.indexOf(POR_MARK);

    if (signatureLocation >= 0) {
        dbgLog.info("format signature was found at:" + signatureLocation);
    } else {
        dbgLog.severe("signature string was not found");
        throw new IOException("signature string was not found");
    }

    // rewind the position to the beginning
    reader.reset();

    // skip bytes up to the signature string
    long skippedBytes = reader.skip(signatureLocation);

    char[] sec2_leader = new char[POR_MARK.length()];
    int nbytes_sec2_leader = reader.read(sec2_leader);

    String leader_string = new String(sec2_leader);

    dbgLog.info("format signature [SPSSPORT] detected=" + leader_string);

    if (leader_string.equals("SPSSPORT")) {
        dbgLog.info("signature was correctly detected");

    } else {
        dbgLog.severe("the format signature is not found at the previously located column");
        throw new IOException("decodeSec2: failed to find the signature string");
    }

    int length_section_2 = LENGTH_SECTION_2;

    char[] Sec2_bytes = new char[length_section_2];

    int nbytes_sec2 = reader.read(Sec2_bytes);

    if (nbytes_sec2 == 0) {
        dbgLog.severe("decodeSec2: reading error");
        throw new IOException("decodeSec2: reading error");
    } else {
        dbgLog.fine("bytes read=" + nbytes_sec2);
    }

    String sec2 = new String(Sec2_bytes);
    dbgLog.fine("sec2[creation date/time]=" + sec2);

    // sec2
    //       0123456789012345678
    //       A8/YYYYMMDD6/HHMMSS
    // thus
    // section2 should has 3 elements

    String[] section2 = StringUtils.split(sec2, '/');

    dbgLog.fine("section2=" + StringUtils.join(section2, "|"));

    String fileCreationDate = null;
    String fileCreationTime = null;
    if ((section2.length == 3) && (section2[0].startsWith("A"))) {
        fileCreationDate = section2[1].substring(0, 7);
        fileCreationTime = section2[2];
    } else {
        dbgLog.severe("decodeSec2: file creation date/time were not correctly detected");
        throw new IOException("decodeSec2: file creation date/time were not correctly detected");
    }
    dbgLog.fine("fileCreationDate=" + fileCreationDate);
    dbgLog.fine("fileCreationTime=" + fileCreationTime);
    smd.getFileInformation().put("fileCreationDate", fileCreationDate);
    smd.getFileInformation().put("fileCreationTime", fileCreationTime);
    smd.getFileInformation().put("varFormat_schema", "SPSS");
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReader.java

private void decodeSec2(BufferedReader reader) throws IOException {
    dbgLog.fine("decodeSec2(): start");
    if (reader == null) {
        throw new IllegalArgumentException("decodeSec2: stream == null!");
    }/*from  w ww . ja  va 2 s  .  c  o  m*/

    // Because a 64-bit machine may not save the first 40
    // bytes of a POR file in a way as a 32-bit machine does,
    // the first 5 lines of a POR file is excluded from the read-back
    // file and the new 1st line contains the format mark "SPSSPORT"
    // somewhere in it.

    // mark the start position for the later rewind
    if (reader.markSupported()) {
        reader.mark(100000);
    }

    char[] sixthLineCharArray = new char[80];
    int nbytes_sixthLine = reader.read(sixthLineCharArray);

    String sixthLine = new String(sixthLineCharArray);
    dbgLog.fine("sixthLineCharArray=" + Arrays.deepToString(ArrayUtils.toObject(sixthLineCharArray)));
    int signatureLocation = sixthLine.indexOf(POR_MARK);

    if (signatureLocation >= 0) {
        dbgLog.fine("format signature was found at:" + signatureLocation);
    } else {
        dbgLog.severe("signature string was not found");
        throw new IOException("signature string was not found");
    }

    // rewind the position to the beginning
    reader.reset();

    // skip bytes up to the signature string
    long skippedBytes = reader.skip(signatureLocation);

    char[] sec2_leader = new char[POR_MARK.length()];
    int nbytes_sec2_leader = reader.read(sec2_leader);

    String leader_string = new String(sec2_leader);

    dbgLog.fine("format signature [SPSSPORT] detected=" + leader_string);

    if (leader_string.equals("SPSSPORT")) {
        dbgLog.fine("signature was correctly detected");

    } else {
        dbgLog.severe("the format signature is not found at the previously located column");
        throw new IOException("decodeSec2: failed to find the signature string");
    }

    int length_section_2 = LENGTH_SECTION_2;

    char[] Sec2_bytes = new char[length_section_2];

    int nbytes_sec2 = reader.read(Sec2_bytes);

    if (nbytes_sec2 == 0) {
        dbgLog.severe("decodeSec2: reading error");
        throw new IOException("decodeSec2: reading error");
    } else {
        dbgLog.fine("bytes read=" + nbytes_sec2);
    }

    String sec2 = new String(Sec2_bytes);
    dbgLog.fine("sec2[creation date/time]=" + sec2);

    // sec2
    //       0123456789012345678
    //       A8/YYYYMMDD6/HHMMSS
    // thus
    // section2 should has 3 elements

    String[] section2 = StringUtils.split(sec2, '/');

    dbgLog.fine("section2=" + StringUtils.join(section2, "|"));

    String fileCreationDate = null;
    String fileCreationTime = null;
    if ((section2.length == 3) && (section2[0].startsWith("A"))) {
        fileCreationDate = section2[1].substring(0, 7);
        fileCreationTime = section2[2];
    } else {
        dbgLog.severe("decodeSec2: file creation date/time were not correctly detected");
        throw new IOException("decodeSec2: file creation date/time were not correctly detected");
    }
    dbgLog.fine("fileCreationDate=" + fileCreationDate);
    dbgLog.fine("fileCreationTime=" + fileCreationTime);
    ///smd.getFileInformation().put("fileCreationDate", fileCreationDate);
    ///smd.getFileInformation().put("fileCreationTime", fileCreationTime);
    ///smd.getFileInformation().put("varFormat_schema", "SPSS");
    dbgLog.fine("decodeSec2(): end");
}