hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
/* Copyright 2017 Google Inc. All Rights Reserved.
 
   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
 
package org.brotli.wrapper.dec;
 
import static org.junit.Assert.assertEquals;
 
import org.brotli.integration.BrotliJniTestBase;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
 
/** Tests for {@link org.brotli.wrapper.dec.BrotliInputStream}. */
@RunWith(JUnit4.class)
public class EagerStreamTest extends BrotliJniTestBase {
 
  @Test
  public void testEagerReading() throws IOException {
    final StringBuilder log = new StringBuilder();
    final byte[] data = {0, 0, 16, 42, 3};
    InputStream source = new InputStream() {
      int index;
 
      @Override
      public int read() {
        if (index < data.length) {
          log.append("<").append(index);
          return data[index++];
        } else {
          log.append("<#");
          return -1;
        }
      }
 
      @Override
      public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
      }
 
      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        if (len < 1) {
          return 0;
        }
        int d = read();
        if (d == -1) {
          return 0;
        }
        b[off] = (byte) d;
        return 1;
      }
    };
    BrotliInputStream reader = new BrotliInputStream(source);
    reader.enableEagerOutput();
    int count = 0;
    while (true) {
      log.append("^").append(count);
      int b = reader.read();
      if (b == -1) {
        log.append(">#");
        break;
      } else {
        log.append(">").append(count++);
      }
    }
    // Lazy log:  ^0<0<1<2<3<4>0^1>#
    assertEquals("^0<0<1<2<3>0^1<4>#", log.toString());
  }
 
}