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 | package org.apache.james.mime4j.io; |
---|
21 | |
---|
22 | import java.io.FilterInputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | |
---|
26 | /** |
---|
27 | * <code>InputStream</code> used by the parser to wrap the original user |
---|
28 | * supplied stream. This stream keeps track of the current line number. |
---|
29 | */ |
---|
30 | public class LineNumberInputStream extends FilterInputStream implements |
---|
31 | LineNumberSource { |
---|
32 | private int lineNumber = 1; |
---|
33 | |
---|
34 | /** |
---|
35 | * Creates a new <code>LineNumberInputStream</code>. |
---|
36 | * |
---|
37 | * @param is |
---|
38 | * the stream to read from. |
---|
39 | */ |
---|
40 | public LineNumberInputStream(InputStream is) { |
---|
41 | super(is); |
---|
42 | } |
---|
43 | |
---|
44 | public int getLineNumber() { |
---|
45 | return lineNumber; |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public int read() throws IOException { |
---|
50 | int b = in.read(); |
---|
51 | if (b == '\n') { |
---|
52 | lineNumber++; |
---|
53 | } |
---|
54 | return b; |
---|
55 | } |
---|
56 | |
---|
57 | @Override |
---|
58 | public int read(byte[] b, int off, int len) throws IOException { |
---|
59 | int n = in.read(b, off, len); |
---|
60 | for (int i = off; i < off + n; i++) { |
---|
61 | if (b[i] == '\n') { |
---|
62 | lineNumber++; |
---|
63 | } |
---|
64 | } |
---|
65 | return n; |
---|
66 | } |
---|
67 | } |
---|