aboutsummaryrefslogtreecommitdiff
path: root/generator/src/main/java/com/google/archivepatcher/generator/MatchingOutputStream.java
blob: f3c536377b052ff1d962d57be55228cd38967d50 (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
// Copyright 2016 Google Inc. All rights reserved.
//
// 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.google.archivepatcher.generator;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * A simple {@link OutputStream} that requires all bytes that are written to match bytes from a
 * specified corresponding {@link InputStream}. Any length or content mismatch results in the stream
 * being closed and an error being raised.
 * <p>
 * Every call to one of the write(...) methods results in reading the same total number of bytes
 * from the {@link InputStream}. The bytes in both streams must match exactly.
 */
public class MatchingOutputStream extends OutputStream {

  /**
   * The bytes to match against.
   */
  private final InputStream expectedBytesStream;

  /**
   * The buffer for reading bytes from the input stream for matching.
   */
  private final byte[] buffer;

  /**
   * Constructs a new stream that will match against the the specified {@link InputStream}.
   * @param expectedBytesStream stream of bytes to expect to see
   * @param matchBufferSize the number of bytes to reserve for matching against the specified
   * {@link InputStream}. This
   */
  public MatchingOutputStream(InputStream expectedBytesStream, int matchBufferSize) {
    if (matchBufferSize < 1) {
      throw new IllegalArgumentException("buffer size must be >= 1");
    }
    this.expectedBytesStream = expectedBytesStream;
    this.buffer = new byte[matchBufferSize];
  }

  @Override
  public void write(int b) throws IOException {
    int expected = expectedBytesStream.read();
    if (expected == -1) {
      throw new MismatchException("EOF reached in expectedBytesStream");
    }
    if (expected != b) {
      throw new MismatchException("Data does not match");
    }
  }

  @Override
  public void write(byte[] b) throws IOException {
    write(b, 0, b.length);
  }

  @Override
  public void write(byte[] dataToWrite, int offset, int length) throws IOException {
    int numReadSoFar = 0;
    while (numReadSoFar < length) {
      int maxToRead = Math.min(buffer.length, length - numReadSoFar);
      int numReadThisLoop = expectedBytesStream.read(buffer, 0, maxToRead);
      if (numReadThisLoop == -1) {
        throw new MismatchException("EOF reached in expectedBytesStream");
      }
      for (int matchCount = 0; matchCount < numReadThisLoop; matchCount++) {
        if (buffer[matchCount] != dataToWrite[offset + numReadSoFar + matchCount]) {
          throw new MismatchException("Data does not match");
        }
      }
      numReadSoFar += numReadThisLoop;
    }
  }

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

  /**
   * Expects the end-of-file to be reached in the associated {@link InputStream}.
   * @throws IOException if the end-of-file has not yet been reached in the associated
   * {@link InputStream}
   */
  public void expectEof() throws IOException {
    if (expectedBytesStream.read() != -1) {
      throw new MismatchException("EOF not reached in expectedBytesStream");
    }
  }
}