<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasBufferedReader.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.analysis.util</a> > <span class="el_source">MtasBufferedReader.java</span></div><h1>MtasBufferedReader.java</h1><pre class="source lang-java linenums">package mtas.analysis.util;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* The Class MtasBufferedReader.
*/
public class MtasBufferedReader extends Reader {
/** The in. */
private Reader in;
/** The cb. */
private char cb[];
/** The n chars. */
private int nChars;
/** The next char. */
private int nextChar;
/** The previous buffer size. */
private int previousBufferSize;
/** The skip LF. */
<span class="nc" id="L34"> private boolean skipLF = false;</span>
/** The default char buffer size. */
<span class="nc" id="L37"> private static int defaultCharBufferSize = 8192;</span>
/** The default expected line length. */
<span class="nc" id="L40"> private static int defaultExpectedLineLength = 80;</span>
/**
* Instantiates a new mtas buffered reader.
*
* @param in the in
* @param sz the sz
*/
public MtasBufferedReader(Reader in, int sz) {
<span class="nc" id="L49"> super(in);</span>
<span class="nc bnc" id="L50" title="All 2 branches missed."> if (sz <= 0)</span>
<span class="nc" id="L51"> throw new IllegalArgumentException("Buffer size <= 0");</span>
<span class="nc" id="L52"> this.in = in;</span>
<span class="nc" id="L53"> cb = new char[sz];</span>
<span class="nc" id="L54"> nextChar = nChars = 0;</span>
<span class="nc" id="L55"> }</span>
/**
* Instantiates a new mtas buffered reader.
*
* @param in the in
*/
public MtasBufferedReader(Reader in) {
<span class="nc" id="L63"> this(in, defaultCharBufferSize);</span>
<span class="nc" id="L64"> }</span>
/**
* Ensure open.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
private void ensureOpen() throws IOException {
<span class="nc bnc" id="L72" title="All 2 branches missed."> if (in == null)</span>
<span class="nc" id="L73"> throw new IOException("Stream closed");</span>
<span class="nc" id="L74"> }</span>
/**
* Fill.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
private void fill() throws IOException {
int n;
<span class="nc" id="L83"> previousBufferSize += nChars;</span>
do {
<span class="nc" id="L85"> n = in.read(cb, 0, cb.length);</span>
<span class="nc bnc" id="L86" title="All 2 branches missed."> } while (n == 0);</span>
<span class="nc bnc" id="L87" title="All 2 branches missed."> if (n > 0) {</span>
<span class="nc" id="L88"> nChars = n;</span>
<span class="nc" id="L89"> nextChar = 0;</span>
}
<span class="nc" id="L91"> }</span>
/*
* (non-Javadoc)
*
* @see java.io.Reader#read()
*/
@Override
public int read() throws IOException {
<span class="nc" id="L100"> synchronized (lock) {</span>
<span class="nc" id="L101"> ensureOpen();</span>
for (;;) {
<span class="nc bnc" id="L103" title="All 2 branches missed."> if (nextChar >= nChars) {</span>
<span class="nc" id="L104"> fill();</span>
<span class="nc bnc" id="L105" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L106"> return -1;</span>
}
<span class="nc bnc" id="L108" title="All 2 branches missed."> if (skipLF) {</span>
<span class="nc" id="L109"> skipLF = false;</span>
<span class="nc bnc" id="L110" title="All 2 branches missed."> if (cb[nextChar] == '\n') {</span>
<span class="nc" id="L111"> nextChar++;</span>
<span class="nc" id="L112"> continue;</span>
}
}
<span class="nc" id="L115"> return cb[nextChar++];</span>
}
<span class="nc" id="L117"> }</span>
}
/**
* Read 1.
*
* @param cbuf the cbuf
* @param off the off
* @param len the len
* @return the int
* @throws IOException Signals that an I/O exception has occurred.
*/
private int read1(char[] cbuf, int off, int len) throws IOException {
<span class="nc bnc" id="L130" title="All 2 branches missed."> if (nextChar >= nChars) {</span>
/*
* If the requested length is at least as large as the buffer, and if
* there is no mark/reset activity, and if line feeds are not being
* skipped, do not bother to copy the characters into the local buffer. In
* this way buffered streams will cascade harmlessly.
*/
<span class="nc bnc" id="L137" title="All 4 branches missed."> if (len >= cb.length && !skipLF) {</span>
<span class="nc" id="L138"> return in.read(cbuf, off, len);</span>
}
<span class="nc" id="L140"> fill();</span>
}
<span class="nc bnc" id="L142" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L143"> return -1;</span>
<span class="nc bnc" id="L144" title="All 2 branches missed."> if (skipLF) {</span>
<span class="nc" id="L145"> skipLF = false;</span>
<span class="nc bnc" id="L146" title="All 2 branches missed."> if (cb[nextChar] == '\n') {</span>
<span class="nc" id="L147"> nextChar++;</span>
<span class="nc bnc" id="L148" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L149"> fill();</span>
<span class="nc bnc" id="L150" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L151"> return -1;</span>
}
}
<span class="nc" id="L154"> int n = Math.min(len, nChars - nextChar);</span>
<span class="nc" id="L155"> System.arraycopy(cb, nextChar, cbuf, off, n);</span>
<span class="nc" id="L156"> nextChar += n;</span>
<span class="nc" id="L157"> return n;</span>
}
/*
* (non-Javadoc)
*
* @see java.io.Reader#read(char[], int, int)
*/
@Override
public int read(char cbuf[], int off, int len) throws IOException {
<span class="nc" id="L167"> synchronized (lock) {</span>
<span class="nc" id="L168"> ensureOpen();</span>
<span class="nc bnc" id="L169" title="All 10 branches missed."> if ((off < 0) || (off > cbuf.length) || (len < 0)</span>
|| ((off + len) > cbuf.length) || ((off + len) < 0)) {
<span class="nc" id="L171"> throw new IndexOutOfBoundsException();</span>
<span class="nc bnc" id="L172" title="All 2 branches missed."> } else if (len == 0) {</span>
<span class="nc" id="L173"> return 0;</span>
}
<span class="nc" id="L176"> int n = read1(cbuf, off, len);</span>
<span class="nc bnc" id="L177" title="All 2 branches missed."> if (n <= 0)</span>
<span class="nc" id="L178"> return n;</span>
<span class="nc bnc" id="L179" title="All 4 branches missed."> while ((n < len) && in.ready()) {</span>
<span class="nc" id="L180"> int n1 = read1(cbuf, off + n, len - n);</span>
<span class="nc bnc" id="L181" title="All 2 branches missed."> if (n1 <= 0)</span>
<span class="nc" id="L182"> break;</span>
<span class="nc" id="L183"> n += n1;</span>
<span class="nc" id="L184"> }</span>
<span class="nc" id="L185"> return n;</span>
<span class="nc" id="L186"> }</span>
}
/**
* Read line.
*
* @param ignoreLF the ignore LF
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
String readLine(boolean ignoreLF) throws IOException {
<span class="nc" id="L197"> StringBuffer s = null;</span>
int startChar;
<span class="nc" id="L200"> synchronized (lock) {</span>
<span class="nc" id="L201"> ensureOpen();</span>
<span class="nc bnc" id="L202" title="All 4 branches missed."> boolean omitLF = ignoreLF || skipLF;</span>
for (;;) {
<span class="nc bnc" id="L206" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L207"> fill();</span>
<span class="nc bnc" id="L208" title="All 2 branches missed."> if (nextChar >= nChars) { /* EOF */</span>
<span class="nc bnc" id="L209" title="All 4 branches missed."> if (s != null && s.length() > 0)</span>
<span class="nc" id="L210"> return s.toString();</span>
else
<span class="nc" id="L212"> return null;</span>
}
<span class="nc" id="L214"> boolean eol = false;</span>
<span class="nc" id="L215"> char c = 0;</span>
int i;
/* Skip a leftover '\n', if necessary */
<span class="nc bnc" id="L219" title="All 4 branches missed."> if (omitLF && (cb[nextChar] == '\n'))</span>
<span class="nc" id="L220"> nextChar++;</span>
<span class="nc" id="L221"> skipLF = false;</span>
<span class="nc" id="L222"> omitLF = false;</span>
<span class="nc bnc" id="L224" title="All 2 branches missed."> charLoop: for (i = nextChar; i < nChars; i++) {</span>
<span class="nc" id="L225"> c = cb[i];</span>
<span class="nc bnc" id="L226" title="All 4 branches missed."> if ((c == '\n') || (c == '\r')) {</span>
<span class="nc" id="L227"> eol = true;</span>
<span class="nc" id="L228"> break charLoop;</span>
}
}
<span class="nc" id="L232"> startChar = nextChar;</span>
<span class="nc" id="L233"> nextChar = i;</span>
<span class="nc bnc" id="L235" title="All 2 branches missed."> if (eol) {</span>
String str;
<span class="nc bnc" id="L237" title="All 2 branches missed."> if (s == null) {</span>
<span class="nc" id="L238"> str = new String(cb, startChar, i - startChar);</span>
} else {
<span class="nc" id="L240"> s.append(cb, startChar, i - startChar);</span>
<span class="nc" id="L241"> str = s.toString();</span>
}
<span class="nc" id="L243"> nextChar++;</span>
<span class="nc bnc" id="L244" title="All 2 branches missed."> if (c == '\r') {</span>
<span class="nc" id="L245"> skipLF = true;</span>
}
<span class="nc" id="L247"> return str;</span>
}
<span class="nc bnc" id="L250" title="All 2 branches missed."> if (s == null)</span>
<span class="nc" id="L251"> s = new StringBuffer(defaultExpectedLineLength);</span>
<span class="nc" id="L252"> s.append(cb, startChar, i - startChar);</span>
<span class="nc" id="L253"> }</span>
<span class="nc" id="L254"> }</span>
}
/**
* Read line.
*
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
public String readLine() throws IOException {
<span class="nc" id="L264"> return readLine(false);</span>
}
/**
* Gets the position.
*
* @return the position
*/
public int getPosition() {
<span class="nc" id="L273"> return previousBufferSize + nextChar;</span>
}
/*
* (non-Javadoc)
*
* @see java.io.Reader#skip(long)
*/
@Override
public long skip(long n) throws IOException {
<span class="nc bnc" id="L283" title="All 2 branches missed."> if (n < 0L) {</span>
<span class="nc" id="L284"> throw new IllegalArgumentException("skip value is negative");</span>
}
<span class="nc" id="L286"> synchronized (lock) {</span>
<span class="nc" id="L287"> ensureOpen();</span>
<span class="nc" id="L288"> long r = n;</span>
<span class="nc bnc" id="L289" title="All 2 branches missed."> while (r > 0) {</span>
<span class="nc bnc" id="L290" title="All 2 branches missed."> if (nextChar >= nChars)</span>
<span class="nc" id="L291"> fill();</span>
<span class="nc bnc" id="L292" title="All 2 branches missed."> if (nextChar >= nChars) /* EOF */</span>
<span class="nc" id="L293"> break;</span>
<span class="nc bnc" id="L294" title="All 2 branches missed."> if (skipLF) {</span>
<span class="nc" id="L295"> skipLF = false;</span>
<span class="nc bnc" id="L296" title="All 2 branches missed."> if (cb[nextChar] == '\n') {</span>
<span class="nc" id="L297"> nextChar++;</span>
}
}
<span class="nc" id="L300"> long d = (long) nChars - nextChar;</span>
<span class="nc bnc" id="L301" title="All 2 branches missed."> if (r <= d) {</span>
<span class="nc" id="L302"> nextChar += r;</span>
<span class="nc" id="L303"> r = 0;</span>
<span class="nc" id="L304"> break;</span>
} else {
<span class="nc" id="L306"> r -= d;</span>
<span class="nc" id="L307"> nextChar = nChars;</span>
}
<span class="nc" id="L309"> }</span>
<span class="nc" id="L310"> return n - r;</span>
<span class="nc" id="L311"> }</span>
}
/*
* (non-Javadoc)
*
* @see java.io.Reader#ready()
*/
@Override
public boolean ready() throws IOException {
<span class="nc" id="L321"> synchronized (lock) {</span>
<span class="nc" id="L322"> ensureOpen();</span>
/*
* If newline needs to be skipped and the next char to be read is a
* newline character, then just skip it right away.
*/
<span class="nc bnc" id="L328" title="All 2 branches missed."> if (skipLF) {</span>
/*
* Note that in.ready() will return true if and only if the next read on
* the stream will not block.
*/
<span class="nc bnc" id="L333" title="All 4 branches missed."> if (nextChar >= nChars && in.ready()) {</span>
<span class="nc" id="L334"> fill();</span>
}
<span class="nc bnc" id="L336" title="All 2 branches missed."> if (nextChar < nChars) {</span>
<span class="nc bnc" id="L337" title="All 2 branches missed."> if (cb[nextChar] == '\n')</span>
<span class="nc" id="L338"> nextChar++;</span>
<span class="nc" id="L339"> skipLF = false;</span>
}
}
<span class="nc bnc" id="L342" title="All 4 branches missed."> return (nextChar < nChars) || in.ready();</span>
<span class="nc" id="L343"> }</span>
}
/*
* (non-Javadoc)
*
* @see java.io.Reader#reset()
*/
@Override
public void reset() throws IOException {
<span class="nc" id="L353"> synchronized (lock) {</span>
<span class="nc" id="L354"> ensureOpen();</span>
<span class="nc" id="L355"> nextChar = -1;</span>
<span class="nc" id="L356"> previousBufferSize = 0;</span>
<span class="nc" id="L357"> }</span>
<span class="nc" id="L358"> }</span>
/*
* (non-Javadoc)
*
* @see java.io.Reader#close()
*/
@Override
public void close() throws IOException {
<span class="nc" id="L367"> synchronized (lock) {</span>
<span class="nc bnc" id="L368" title="All 2 branches missed."> if (in == null)</span>
<span class="nc" id="L369"> return;</span>
try {
<span class="nc" id="L371"> in.close();</span>
} finally {
<span class="nc" id="L373"> in = null;</span>
<span class="nc" id="L374"> cb = null;</span>
<span class="nc" id="L375"> }</span>
<span class="nc" id="L376"> }</span>
<span class="nc" id="L377"> }</span>
/**
* Lines.
*
* @return the stream
*/
public Stream<String> lines() {
<span class="nc" id="L385"> Iterator<String> iter = new Iterator<String>() {</span>
<span class="nc" id="L386"> String nextLine = null;</span>
@Override
public boolean hasNext() {
<span class="nc bnc" id="L390" title="All 2 branches missed."> if (nextLine != null) {</span>
<span class="nc" id="L391"> return true;</span>
} else {
try {
<span class="nc" id="L394"> nextLine = readLine();</span>
<span class="nc bnc" id="L395" title="All 2 branches missed."> return (nextLine != null);</span>
<span class="nc" id="L396"> } catch (IOException e) {</span>
<span class="nc" id="L397"> throw new UncheckedIOException(e);</span>
}
}
}
@Override
public String next() {
<span class="nc bnc" id="L404" title="All 4 branches missed."> if (nextLine != null || hasNext()) {</span>
<span class="nc" id="L405"> String line = nextLine;</span>
<span class="nc" id="L406"> nextLine = null;</span>
<span class="nc" id="L407"> return line;</span>
} else {
<span class="nc" id="L409"> throw new NoSuchElementException();</span>
}
}
};
<span class="nc" id="L413"> return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iter,</span>
Spliterator.ORDERED | Spliterator.NONNULL), false);
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>