01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.luni.util;
19:
20: import java.io.FilterInputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23:
24: /**
25: * This class implements a Stream whose position can be queried for.
26: */
27: public class PositionedInputStream extends FilterInputStream {
28:
29: private int currentPosition; // Current position on the underlying stream
30:
31: /**
32: * Constructs a new instance of the receiver.
33: *
34: * @param in The actual input stream where to read the bytes from.
35: */
36: public PositionedInputStream(InputStream in) {
37: super (in);
38: }
39:
40: /**
41: * Return the current position in the receiver
42: *
43: * @return int The current position in the receiver
44: */
45: public int currentPosition() {
46: return currentPosition;
47: }
48:
49: @Override
50: public int read() throws IOException {
51: int read = in.read();
52: if (read >= 0) {
53: currentPosition++;
54: }
55: return read;
56:
57: }
58:
59: @Override
60: public int read(byte b[], int off, int len) throws IOException {
61: int read = in.read(b, off, len);
62: if (read >= 0) {
63: currentPosition += read;
64: }
65: return read;
66: }
67:
68: /**
69: * Makes the current position on the underlying stream be assigned relative
70: * position zero.
71: */
72: public void resetCurrentPosition() {
73: currentPosition = 0;
74: }
75:
76: @Override
77: public long skip(long n) throws IOException {
78: long skip = in.skip(n);
79: currentPosition += skip; // Maybe currentPosition should be long ?
80: return skip;
81: }
82: }
|