2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.rmi.log;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
|
|
30 |
public
|
|
31 |
class LogInputStream extends InputStream {
|
|
32 |
private InputStream in;
|
|
33 |
private int length;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Creates a log input file with the specified system dependent
|
|
37 |
* file descriptor.
|
|
38 |
* @param fd the system dependent file descriptor
|
|
39 |
* @param length the total number of bytes allowed to be read
|
|
40 |
* @exception IOException If an I/O error has occurred.
|
|
41 |
*/
|
|
42 |
public LogInputStream(InputStream in, int length) throws IOException {
|
|
43 |
this.in = in;
|
|
44 |
this.length = length;
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Reads a byte of data. This method will block if no input is
|
|
49 |
* available.
|
|
50 |
* @return the byte read, or -1 if the end of the log or end of the
|
|
51 |
* stream is reached.
|
|
52 |
* @exception IOException If an I/O error has occurred.
|
|
53 |
*/
|
|
54 |
public int read() throws IOException {
|
|
55 |
if (length == 0)
|
|
56 |
return -1;
|
|
57 |
int c = in.read();
|
|
58 |
length = (c != -1) ? length - 1 : 0;
|
|
59 |
return c;
|
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Reads data into an array of bytes.
|
|
64 |
* This method blocks until some input is available.
|
|
65 |
* @param b the buffer into which the data is read
|
|
66 |
* @return the actual number of bytes read, or -1 if the end of the log
|
|
67 |
* or end of the stream is reached.
|
|
68 |
* @exception IOException If an I/O error has occurred.
|
|
69 |
*/
|
|
70 |
public int read(byte b[]) throws IOException {
|
|
71 |
return read(b, 0, b.length);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Reads data into an array of bytes.
|
|
76 |
* This method blocks until some input is available.
|
|
77 |
* @param b the buffer into which the data is read
|
|
78 |
* @param off the start offset of the data
|
|
79 |
* @param len the maximum number of bytes read
|
|
80 |
* @return the actual number of bytes read, or -1 if the end of the log or
|
|
81 |
* end of the stream is reached.
|
|
82 |
* @exception IOException If an I/O error has occurred.
|
|
83 |
*/
|
|
84 |
public int read(byte b[], int off, int len) throws IOException {
|
|
85 |
if (length == 0)
|
|
86 |
return -1;
|
|
87 |
len = (length < len) ? length : len;
|
|
88 |
int n = in.read(b, off, len);
|
|
89 |
length = (n != -1) ? length - n : 0;
|
|
90 |
return n;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Skips n bytes of input.
|
|
95 |
* @param n the number of bytes to be skipped
|
|
96 |
* @return the actual number of bytes skipped.
|
|
97 |
* @exception IOException If an I/O error has occurred.
|
|
98 |
*/
|
|
99 |
public long skip(long n) throws IOException {
|
|
100 |
if (n > Integer.MAX_VALUE)
|
|
101 |
throw new IOException("Too many bytes to skip - " + n);
|
|
102 |
if (length == 0)
|
|
103 |
return 0;
|
|
104 |
n = (length < n) ? length : n;
|
|
105 |
n = in.skip(n);
|
|
106 |
length -= n;
|
|
107 |
return n;
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Returns the number of bytes that can be read without blocking.
|
|
112 |
* @return the number of available bytes, which is initially
|
|
113 |
* equal to the file size.
|
|
114 |
*/
|
|
115 |
public int available() throws IOException {
|
|
116 |
int avail = in.available();
|
|
117 |
return (length < avail) ? length : avail;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Closes the input stream. No further input can be read.
|
|
122 |
* the stream.
|
|
123 |
*/
|
|
124 |
public void close() {
|
|
125 |
length = 0;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Closes the stream when garbage is collected.
|
|
130 |
*/
|
|
131 |
protected void finalize() throws IOException {
|
|
132 |
close();
|
|
133 |
}
|
|
134 |
}
|