source: trunk/src/gcc/libjava/java/io/LineNumberInputStream.java@ 228

Last change on this file since 228 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.1 KB
Line 
1/* Copyright (C) 1998, 1999 Free Software Foundation
2
3 This file is part of libgcj.
4
5This software is copyrighted work licensed under the terms of the
6Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7details. */
8
9package java.io;
10
11/**
12 * @author Warren Levy <[email protected]>
13 * @date November 11, 1998.
14 * @deprecated
15 */
16/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
17 * "The Java Language Specification", ISBN 0-201-63451-1
18 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
19 * Status: Believed complete and correct. Deprecated in JDK 1.1.
20 */
21
22public class LineNumberInputStream extends FilterInputStream
23{
24 /* The current line number. */
25 private int lineNumber = 0;
26
27 /* The line number when the stream was marked. */
28 private int markLineNumber = 0;
29
30 /* Flag to indicate a '\r' was just read so that an immediately subsequent
31 * '\n' can be ignored. */
32 private boolean justReadReturnChar = false;
33
34 public LineNumberInputStream(InputStream in)
35 {
36 super(in);
37 }
38
39 public int available() throws IOException
40 {
41 // We can only guarantee half the characters that might be available
42 // without blocking because "\r\n" is treated as a single character.
43 return in.available() / 2;
44 }
45
46 public int getLineNumber()
47 {
48 return lineNumber;
49 }
50
51 public void mark(int readlimit)
52 {
53 in.mark(readlimit);
54 markLineNumber = lineNumber;
55 }
56
57 public int read() throws IOException
58 {
59 // Treat "\r\n" as a single character. A '\r' may have been read by
60 // a previous call to read so we keep an internal flag to avoid having
61 // to read ahead.
62
63 int ch = in.read();
64
65 if (ch == '\n')
66 if (justReadReturnChar)
67 {
68 ch = in.read();
69 justReadReturnChar = false;
70 }
71 else
72 lineNumber++;
73 else if (ch == '\r')
74 {
75 ch = '\n';
76 justReadReturnChar = true;
77 lineNumber++;
78 }
79 else
80 justReadReturnChar = false;
81
82 return ch;
83 }
84
85 public int read(byte[] b, int off, int len) throws IOException
86 {
87 if (off < 0 || len < 0 || off + len > b.length)
88 throw new ArrayIndexOutOfBoundsException();
89
90 // This case always succeeds.
91 if (len == 0)
92 return 0;
93
94 // The simplest, though not necessarily the most time efficient thing
95 // to do is simply call read(void) len times. Since this is a deprecated
96 // class, that should be ok.
97 final int origOff = off;
98 while (len-- > 0)
99 {
100 int ch = read();
101 if (ch < 0)
102 break;
103
104 b[off++] = (byte) ch;
105 }
106
107 // This is safe since we already know that some bytes were
108 // actually requested.
109 return off == origOff ? -1 : off - origOff;
110 }
111
112 public void reset() throws IOException
113 {
114 in.reset();
115 lineNumber = markLineNumber;
116 justReadReturnChar = false;
117 }
118
119 public void setLineNumber(int lineNumber)
120 {
121 this.lineNumber = lineNumber;
122 }
123
124 public long skip(long n) throws IOException
125 {
126 if (n <= 0)
127 return 0L;
128
129 final long origN = n;
130
131 do
132 {
133 int ch = read();
134 if (ch < 0)
135 break;
136 if (ch == '\n' || ch == '\r')
137 lineNumber++;
138 }
139 while (--n > 0);
140
141 return origN - n;
142 }
143}
Note: See TracBrowser for help on using the repository browser.