aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/code_intelligence/jazzer/mutation/support/InputStreamSupport.java
blob: e0902428ab51f9222a30426e6be9bfe995967b36 (plain)
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
/*
 * Copyright 2023 Code Intelligence GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.code_intelligence.jazzer.mutation.support;

import static com.code_intelligence.jazzer.mutation.support.Preconditions.require;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

public final class InputStreamSupport {
  public static byte[] readAllBytes(InputStream stream) throws IOException {
    requireNonNull(stream);
    Queue<byte[]> buffers = new ArrayDeque<>();
    int arrayLength = 0;
    outer:
    while (true) {
      byte[] buffer = new byte[max(8192, stream.available())];
      buffers.add(buffer);
      int off = 0;
      while (off < buffer.length) {
        int bytesRead = stream.read(buffer, off, buffer.length - off);
        if (bytesRead == -1) {
          break outer;
        }
        off += bytesRead;
        arrayLength += bytesRead;
      }
    }

    byte[] result = new byte[arrayLength];
    int offset = 0;
    byte[] buffer;
    int remaining = arrayLength;
    while ((buffer = buffers.poll()) != null) {
      int toCopy = min(buffer.length, remaining);
      System.arraycopy(buffer, 0, result, offset, toCopy);
      remaining -= toCopy;
    }
    return result;
  }

  private static final InputStream infiniteZerosStream = new ExtendWithNullInputStream();

  /**
   * @return an infinite stream consisting of 0s
   */
  public static InputStream infiniteZeros() {
    return infiniteZerosStream;
  }

  /**
   * @return {@code stream} extended with 0s to an infinite stream
   */
  public static InputStream extendWithZeros(InputStream stream) {
    if (stream instanceof ExtendWithNullInputStream) {
      return stream;
    }
    return new ExtendWithNullInputStream(requireNonNull(stream));
  }

  public static final class ExtendWithNullInputStream extends InputStream {
    private static final InputStream ALWAYS_EOF = new ByteArrayInputStream(new byte[0]);
    private final InputStream stream;
    private boolean eof;

    private ExtendWithNullInputStream() {
      this.stream = ALWAYS_EOF;
      this.eof = true;
    }

    private ExtendWithNullInputStream(InputStream stream) {
      this.stream = stream;
      this.eof = false;
    }

    @Override
    public int read() throws IOException {
      if (eof) {
        return 0;
      }

      int res = stream.read();
      if (res != -1) {
        return res;
      } else {
        eof = true;
        return 0;
      }
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      if (eof) {
        Arrays.fill(b, off, off + len, (byte) 0);
      } else {
        int bytesRead = stream.read(b, off, len);
        if (bytesRead < len) {
          eof = true;
          Arrays.fill(b, max(off, off + bytesRead), off + len, (byte) 0);
        }
      }
      return len;
    }

    @Override
    public int available() throws IOException {
      if (eof) {
        return Integer.MAX_VALUE;
      } else {
        return stream.available();
      }
    }

    @Override
    public void close() throws IOException {
      stream.close();
    }
  }

  /**
   * @return a stream with the first {@code bytes} bytes of {@code stream}
   */
  public static InputStream cap(InputStream stream, long bytes) {
    requireNonNull(stream);
    require(bytes >= 0, "bytes must be non-negative");
    return new CappedInputStream(stream, bytes);
  }

  private static final class CappedInputStream extends InputStream {
    private final InputStream stream;
    private long remaining;

    CappedInputStream(InputStream stream, long remaining) {
      this.stream = stream;
      this.remaining = remaining;
    }

    @Override
    public int read() throws IOException {
      if (remaining == 0) {
        return -1;
      }

      int res = stream.read();
      if (res != -1) {
        --remaining;
      }
      return res;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      if (remaining == 0) {
        return -1;
      }

      int res = stream.read(b, off, (int) min(len, remaining));
      if (res != -1) {
        remaining -= res;
      }
      return res;
    }

    @Override
    public int available() throws IOException {
      return (int) min(stream.available(), remaining);
    }

    @Override
    public void close() throws IOException {
      stream.close();
    }
  }

  /**
   * Wraps a given stream with the functionality to detect if it was read exactly. To do so, the
   * stream must provide an accurate implementation of {@link InputStream#available()}, hence it's
   * restricted to {@link ByteArrayInputStream} for now.
   *
   * @return {@code stream} extended that detects if it was consumed exactly
   */
  public static ReadExactlyInputStream extendWithReadExactly(ByteArrayInputStream stream) {
    return new ReadExactlyInputStream(requireNonNull(stream));
  }

  public static final class ReadExactlyInputStream extends InputStream {
    private final InputStream stream;
    private boolean eof;

    private ReadExactlyInputStream(InputStream stream) {
      this.stream = stream;
      this.eof = false;
    }

    public boolean isConsumedExactly() {
      try {
        // Forwards availability check to the underlying ByteInputStream,
        // which is accurate for the number of available bytes.
        return !eof && available() == 0;
      } catch (IOException e) {
        return false;
      }
    }

    @Override
    public int read() throws IOException {
      int res = stream.read();
      if (res == -1) {
        eof = true;
      }
      return res;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      int read = stream.read(b, off, len);
      if (read < len) {
        eof = true;
      }
      return read;
    }

    @Override
    public int available() throws IOException {
      return stream.available();
    }

    @Override
    public void close() throws IOException {
      stream.close();
    }
  }

  private InputStreamSupport() {}
}