source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/core/src/main/java/org/apache/james/mime4j/io/LineNumberInputStream.java @ 6785

Revision 6785, 2.3 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.io;
21
22import java.io.FilterInputStream;
23import java.io.IOException;
24import 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 */
30public 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}
Note: See TracBrowser for help on using the repository browser.