MtasBufferedReader.java.html 17.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
<?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> &gt; <a href="index.source.html" class="el_package">mtas.analysis.util</a> &gt; <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 &lt;= 0)</span>
<span class="nc" id="L51">      throw new IllegalArgumentException(&quot;Buffer size &lt;= 0&quot;);</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(&quot;Stream closed&quot;);</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 &gt; 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 &gt;= nChars) {</span>
<span class="nc" id="L104">          fill();</span>
<span class="nc bnc" id="L105" title="All 2 branches missed.">          if (nextChar &gt;= 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 &gt;= 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 &gt;= cb.length &amp;&amp; !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 &gt;= 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 &gt;= nChars)</span>
<span class="nc" id="L149">          fill();</span>
<span class="nc bnc" id="L150" title="All 2 branches missed.">        if (nextChar &gt;= 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 &lt; 0) || (off &gt; cbuf.length) || (len &lt; 0)</span>
          || ((off + len) &gt; cbuf.length) || ((off + len) &lt; 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 &lt;= 0)</span>
<span class="nc" id="L178">        return n;</span>
<span class="nc bnc" id="L179" title="All 4 branches missed.">      while ((n &lt; len) &amp;&amp; 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 &lt;= 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 &gt;= nChars)</span>
<span class="nc" id="L207">          fill();</span>
<span class="nc bnc" id="L208" title="All 2 branches missed.">        if (nextChar &gt;= nChars) { /* EOF */</span>
<span class="nc bnc" id="L209" title="All 4 branches missed.">          if (s != null &amp;&amp; s.length() &gt; 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 &amp;&amp; (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 &lt; 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 &lt; 0L) {</span>
<span class="nc" id="L284">      throw new IllegalArgumentException(&quot;skip value is negative&quot;);</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 &gt; 0) {</span>
<span class="nc bnc" id="L290" title="All 2 branches missed.">        if (nextChar &gt;= nChars)</span>
<span class="nc" id="L291">          fill();</span>
<span class="nc bnc" id="L292" title="All 2 branches missed.">        if (nextChar &gt;= 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 &lt;= 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 &gt;= nChars &amp;&amp; in.ready()) {</span>
<span class="nc" id="L334">          fill();</span>
        }
<span class="nc bnc" id="L336" title="All 2 branches missed.">        if (nextChar &lt; 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 &lt; 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&lt;String&gt; lines() {
<span class="nc" id="L385">    Iterator&lt;String&gt; iter = new Iterator&lt;String&gt;() {</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>