summaryrefslogtreecommitdiffstats
path: root/mgrapp/src/com/google/codereview/manager/unpack/RecordInputStream.java
blob: 3fe0974963c5c5fa0cbdce1b99606d3097c37822 (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
// Copyright 2008 Google Inc.
//
// 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.codereview.manager.unpack;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/** Buffering input stream which can read entire lines. */
class RecordInputStream extends InputStream {
  private InputStream in;
  private byte[] buf;
  private int pos;
  private int end;

  RecordInputStream(final InputStream in) {
    this.in = in;
    buf = new byte[4096];
  }

  @Override
  public void close() throws IOException {
    try {
      in.close();
    } finally {
      in = null;
      buf = null;
    }
  }

  private boolean fill() throws IOException {
    pos = 0;
    end = in.read(buf, pos, buf.length);
    if (end < 0) {
      end = 0;
      return false;
    }
    return true;
  }

  @Override
  public int read() throws IOException {
    if (pos == end && !fill()) {
      return -1;
    }
    return buf[pos++];
  }

  @Override
  public int read(final byte[] b, final int off, final int len)
      throws IOException {
    if (pos == end && !fill()) {
      return -1;
    }
    final int cnt = Math.min(len, end - pos);
    System.arraycopy(buf, pos, b, off, cnt);
    pos += cnt;
    return cnt;
  }

  /**
   * Read a record terminated by the separator byte.
   * 
   * @param sep byte which delimits the end of a record.
   * @return record content, with the separator removed from the end. The empty
   *         array indicates an empty record; null indicates stream EOF.
   * @throws IOException the stream could not be read from.
   */
  byte[] readRecord(final int sep) throws IOException {
    if (pos == end && !fill()) {
      return null;
    }

    byte[] line = fastReadRecord(sep);
    if (line != null) {
      return line;
    }

    if (end - pos <= buf.length / 2) {
      int cnt = end - pos;
      System.arraycopy(buf, pos, buf, 0, cnt);
      pos = 0;
      end = cnt;

      cnt = in.read(buf, end, buf.length - end);
      if (cnt < 0) {
        final byte[] r = new byte[end];
        System.arraycopy(buf, 0, r, 0, end);
        pos = end = 0;
        return r;
      }
      end += cnt;

      line = fastReadRecord(sep);
      if (line != null) {
        return line;
      }
    }

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(buf, pos, end - pos);
    for (;;) {
      if (!fill()) {
        return out.toByteArray();
      }

      for (int lf = pos; lf < end; lf++) {
        if (buf[lf] == sep) {
          out.write(buf, pos, lf - pos);
          pos = lf + 1;
          return out.toByteArray();
        }
      }

      out.write(buf, pos, end - pos);
      pos = end;
    }
  }

  private byte[] fastReadRecord(final int sep) {
    for (int lf = pos; lf < end; lf++) {
      if (buf[lf] == sep) {
        final int cnt = lf - pos;
        final byte[] r = new byte[cnt];
        System.arraycopy(buf, pos, r, 0, cnt);
        pos = lf + 1;
        return r;
      }
    }
    return null;
  }
}