1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | |
---|
21 | package org.apache.james.mime4j.io; |
---|
22 | |
---|
23 | import java.io.FilterInputStream; |
---|
24 | import java.io.InputStream; |
---|
25 | import java.io.IOException; |
---|
26 | |
---|
27 | public class PositionInputStream extends FilterInputStream { |
---|
28 | |
---|
29 | protected long position = 0; |
---|
30 | private long markedPosition = 0; |
---|
31 | |
---|
32 | public PositionInputStream(InputStream inputStream) { |
---|
33 | super(inputStream); |
---|
34 | } |
---|
35 | |
---|
36 | public long getPosition() { |
---|
37 | return position; |
---|
38 | } |
---|
39 | |
---|
40 | @Override |
---|
41 | public int available() throws IOException { |
---|
42 | return in.available(); |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | public int read() throws IOException { |
---|
47 | int b = in.read(); |
---|
48 | if (b != -1) |
---|
49 | position++; |
---|
50 | return b; |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public void close() throws IOException { |
---|
55 | in.close(); |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public void reset() throws IOException { |
---|
60 | in.reset(); |
---|
61 | position = markedPosition; |
---|
62 | } |
---|
63 | |
---|
64 | @Override |
---|
65 | public boolean markSupported() { |
---|
66 | return in.markSupported(); |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public void mark(int readlimit) { |
---|
71 | in.mark(readlimit); |
---|
72 | markedPosition = position; |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | public long skip(long n) throws IOException { |
---|
77 | final long c = in.skip(n); |
---|
78 | if (c > 0) |
---|
79 | position += c; |
---|
80 | return c; |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public int read(byte b[], int off, int len) throws IOException { |
---|
85 | final int c = in.read(b, off, len); |
---|
86 | if (c > 0) |
---|
87 | position += c; |
---|
88 | return c; |
---|
89 | } |
---|
90 | |
---|
91 | } |
---|