Java Code Examples for java.io.BufferedReader#skip()

The following examples show how to use java.io.BufferedReader#skip() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: training   File: HtmlPagesConverter.java    License: MIT License 6 votes vote down vote up
public String getHtmlPage(int page) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(this.filename));
    reader.skip(breaks.get(page));
    StringBuffer htmlPage = new StringBuffer();
    String line = reader.readLine();
    while (line != null)
    {
        if (line.contains("PAGE_BREAK")) {
            break;
        }
        htmlPage.append(StringEscapeUtils.escapeHtml(line));
        htmlPage.append("<br />");
        
        line = reader.readLine();
    }
    reader.close();
    return htmlPage.toString();
}
 
Example 2
@Override
public long skip(long n) throws IOException
{
  BufferedReader reader = _bufferedReader;
  
  if (reader != null)
    return reader.skip(n);
  
  long count = 0;

  for (; count < n && _rs.readChar() >= 0; count++) {
  }

  return count;
}
 
Example 3
private void copyReader(BufferedReader reader, String path, long startOffset) throws IOException {
  markStartOfFile(path);

  if(startOffset > 0) {
    reader.skip(startOffset);
  }

  String line = null;
  while ((line = reader.readLine()) != null) {
    writeLn(line);
  }

  markEndOfFile();
}
 
Example 4
/**
 * Read file as string.
 *
 * @param filePath the file path
 * @param skip the skip
 * @param bytesCount the bytes count
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void readFileAsString(String filePath, long skip, long bytesCount)
		throws java.io.IOException {
	StringBuilder fileData = new StringBuilder();
	BufferedReader reader = new BufferedReader(new FileReader(filePath));
	long actualSkip = reader.skip(skip);
	if (skip > actualSkip) {
		System.err.println("Invallid skip count");
		reader.close();
		System.exit(1);
	}
	char[] buf = new char[1024];
	int numRead = 0;
	int count = 0;
	while ((numRead = reader.read(buf)) != -1) {
		
		if (bytesCount > 0 && count+numRead >= bytesCount) {
			numRead = (int) bytesCount - count;				
		}
		count += numRead;
		
		String readData = String.valueOf(buf, 0, numRead);
		fileData.append(readData);
		buf = new char[1024];
		if (bytesCount != 0 && count >= bytesCount) {
			break;
		}
	}
	skip += count;
	reader.close();
	System.out.println(skip);
	if (!fileData.toString().isEmpty()) {
		System.out.println(fileData.toString());
	}

}
 
Example 5
private void commonInit(Path filePath, Configuration conf) throws IOException {

		readerPosition = start;

		FileSystem fs = filePath.getFileSystem(conf);
		inputReader = new BufferedReader(new InputStreamReader(fs.open(filePath)));

		if (start != 0) {
			// split starts inside the json
			inputReader.skip(start);
			moveToRecordStart();
		}

	}
 
Example 6
/**
 *  Load the sequence
 * @return
 */
private boolean init() throws IOException, CompoundNotFoundException {

	BufferedReader br = new BufferedReader(new FileReader(file));
	br.skip(sequenceStartIndex);
	String sequence = sequenceParser.getSequence(br, sequenceLength);
	setContents(sequence);
	br.close(); // close file to prevent too many being open

	return true;
}
 
Example 7
Source Project: jstorm   File: TextFileReader.java    License: Apache License 2.0 5 votes vote down vote up
private TextFileReader(FileSystem fs, Path file, Map conf, TextFileReader.Offset startOffset)
        throws IOException {
  super(fs, file);
  offset = startOffset;
  FSDataInputStream in = fs.open(file);

  String charSet = (conf==null || !conf.containsKey(CHARSET) ) ? "UTF-8" : conf.get(CHARSET).toString();
  int buffSz = (conf==null || !conf.containsKey(BUFFER_SIZE) ) ? DEFAULT_BUFF_SIZE : Integer.parseInt( conf.get(BUFFER_SIZE).toString() );
  reader = new BufferedReader(new InputStreamReader(in, charSet), buffSz);
  if(offset.charOffset >0) {
    reader.skip(offset.charOffset);
  }

}
 
Example 8
private void initConsistentReader() throws IOException {
    consistentReader = new BufferedReader(createReader());
    if (isSkipFirstLine && !this.offsets.isEmpty()) {
        consistentReader.skip(this.offsets.get(0)); //TODO: any other ideas how skip header?
    }
}
 
Example 9
Source Project: AndroidHttpServer   File: NanoHTTPD.java    License: MIT License 4 votes vote down vote up
/**
 * Decodes the Multipart Body data and put it into Key/Value pairs.
 */
private void decodeMultipartFormData(String boundary, ByteBuffer fbuf, Map<String, String> parms, Map<String, String> files) throws ResponseException {
    try {
        int[] boundary_idxs = getBoundaryPositions(fbuf, boundary.getBytes());
        if (boundary_idxs.length < 2) {
            throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but contains less than two boundary strings.");
        }

        final int MAX_HEADER_SIZE = 1024;
        byte[] part_header_buff = new byte[MAX_HEADER_SIZE];
        for (int bi = 0; bi < boundary_idxs.length - 1; bi++) {
            fbuf.position(boundary_idxs[bi]);
            int len = (fbuf.remaining() < MAX_HEADER_SIZE) ? fbuf.remaining() : MAX_HEADER_SIZE;
            fbuf.get(part_header_buff, 0, len);
            ByteArrayInputStream bais = new ByteArrayInputStream(part_header_buff, 0, len);
            BufferedReader in = new BufferedReader(new InputStreamReader(bais, Charset.forName("US-ASCII")));

            // First line is boundary string
            String mpline = in.readLine();
            if (!mpline.contains(boundary)) {
                throw new ResponseException(Response.Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but chunk does not start with boundary.");
            }

            String part_name = null, file_name = null, content_type = null;
            // Parse the reset of the header lines
            mpline = in.readLine();
            while (mpline != null && mpline.trim().length() > 0) {
                Matcher matcher = CONTENT_DISPOSITION_PATTERN.matcher(mpline);
                if (matcher.matches()) {
                    String attributeString = matcher.group(2);
                    matcher = CONTENT_DISPOSITION_ATTRIBUTE_PATTERN.matcher(attributeString);
                    while (matcher.find()) {
                        String key = matcher.group(1);
                        if (key.equalsIgnoreCase("name")) {
                            part_name = matcher.group(2);
                        } else if (key.equalsIgnoreCase("filename")) {
                            file_name = matcher.group(2);
                        }
                    }
                }
                matcher = CONTENT_TYPE_PATTERN.matcher(mpline);
                if (matcher.matches()) {
                    content_type = matcher.group(2).trim();
                }
                mpline = in.readLine();
            }

            // Read the part data
            int part_header_len = len - (int) in.skip(MAX_HEADER_SIZE);
            if (part_header_len >= len - 4) {
                throw new ResponseException(Response.Status.INTERNAL_ERROR, "Multipart header size exceeds MAX_HEADER_SIZE.");
            }
            int part_data_start = boundary_idxs[bi] + part_header_len;
            int part_data_end = boundary_idxs[bi + 1] - 4;

            fbuf.position(part_data_start);
            if (content_type == null) {
                // Read the part into a string
                byte[] data_bytes = new byte[part_data_end - part_data_start];
                fbuf.get(data_bytes);
                parms.put(part_name, new String(data_bytes));
            } else {
                // Read it into a file
                String path = saveTmpFile(fbuf, part_data_start, part_data_end - part_data_start);
                if (!files.containsKey(part_name)) {
                    files.put(part_name, path);
                } else {
                    int count = 2;
                    while (files.containsKey(part_name + count)) {
                        count++;
                    }
                    files.put(part_name + count, path);
                }
                parms.put(part_name, file_name);
            }
        }
    } catch (ResponseException re) {
        throw re;
    } catch (Exception e) {
        throw new ResponseException(Response.Status.INTERNAL_ERROR, e.toString());
    }
}
 
Example 10
/**
 * Loads track from file.
 * 
 * @param file valid <code>File</code> with track points
 * @param lines number of last lines to read
 * @return <code>Track</code> with track points
 * @throws IOException on file read error
 * @throws IllegalArgumentException if file format is not plt
 */
public static Track loadTrackFromFile(final File file, final String charset, final long lines) throws IllegalArgumentException, IOException
{
	Track track = new Track();
	
	long skip = 0;
	if (lines > 0)
	{
		skip = file.length() - 35 * lines; // 35 - average line length in conventional track file
	}

    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));

    String line = null;

    // OziExplorer Track Point File Version 2.0
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
    // WGS 84
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
    // Altitude is in Feet
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
    // Reserved 3
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
    // 0,2,255,OziCE Track Log File,1
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
	String[] fields = CSV.parseLine(line);
    if (fields.length < 4)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
	track.width=Integer.parseInt(fields[1]);
	track.color=bgr2rgb(Integer.parseInt(fields[2]));
	track.name=fields[3];
    // 0
    if ((line = reader.readLine()) == null)
    {
    	reader.close();
    	throw new IllegalArgumentException("Bad track file");
    }
    skip -= line.length();
    skip -= 12; // new line characters
	
	if (skip > 0)
	{
		reader.skip(skip);
		reader.readLine(); // skip broken line
	}

    //   55.6384683,  37.3516133,0,    583.0,    0.0000000 ,290705,185332.996
    while ((line = reader.readLine()) != null)
	{
		fields = CSV.parseLine(line);
		long time = fields.length > 4 ? TDateTime.fromDateTime(Double.parseDouble(fields[4])): 0L;
		double elevation = fields.length > 3 ? Double.parseDouble(fields[3]) * 0.3048: 0;
		if (fields.length >= 3)
			track.addPoint("0".equals(fields[2]) ? true : false, Double.parseDouble(fields[0]), Double.parseDouble(fields[1]), elevation, 0.0, 0.0, 0.0, time);
    }
	reader.close();
	
	track.show = true;
	track.filepath = file.getCanonicalPath();
	if ("".equals(track.name))
		track.name = track.filepath;

	return track;
}