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.util;
22 
23 import java.io.InputStream;
24 import java.io.IOException;
25 
26 public class PositionInputStream extends InputStream {
27 
28     private final InputStream inputStream;
29     protected long position = 0;
30     private long markedPosition = 0;
31 
PositionInputStream(InputStream inputStream)32     public PositionInputStream(InputStream inputStream) {
33         this.inputStream = inputStream;
34     }
35 
getPosition()36     public long getPosition() {
37         return position;
38     }
39 
available()40     public int available() throws IOException {
41         return inputStream.available();
42     }
43 
read()44     public int read() throws IOException {
45         int b = inputStream.read();
46         if (b != -1)
47             position++;
48         return b;
49     }
50 
close()51     public void close() throws IOException {
52         inputStream.close();
53     }
54 
reset()55     public void reset() throws IOException {
56         inputStream.reset();
57         position = markedPosition;
58     }
59 
markSupported()60     public boolean markSupported() {
61         return inputStream.markSupported();
62     }
63 
mark(int readlimit)64     public void mark(int readlimit) {
65         inputStream.mark(readlimit);
66         markedPosition = position;
67     }
68 
skip(long n)69     public long skip(long n) throws IOException {
70         final long c = inputStream.skip(n);
71         position += c;
72         return c;
73     }
74 
read(byte b[])75     public int read(byte b[]) throws IOException {
76         final int c = inputStream.read(b);
77         position += c;
78         return c;
79     }
80 
read(byte b[], int off, int len)81     public int read(byte b[], int off, int len) throws IOException {
82         final int c = inputStream.read(b, off, len);
83         position += c;
84         return c;
85     }
86 
87 }
88