Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/log/LogInputStream.java
38919 views
/*1* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.rmi.log;2627import java.io.*;2829public30class LogInputStream extends InputStream {31private InputStream in;32private int length;3334/**35* Creates a log input file with the specified system dependent36* file descriptor.37* @param fd the system dependent file descriptor38* @param length the total number of bytes allowed to be read39* @exception IOException If an I/O error has occurred.40*/41public LogInputStream(InputStream in, int length) throws IOException {42this.in = in;43this.length = length;44}4546/**47* Reads a byte of data. This method will block if no input is48* available.49* @return the byte read, or -1 if the end of the log or end of the50* stream is reached.51* @exception IOException If an I/O error has occurred.52*/53public int read() throws IOException {54if (length == 0)55return -1;56int c = in.read();57length = (c != -1) ? length - 1 : 0;58return c;59}6061/**62* Reads data into an array of bytes.63* This method blocks until some input is available.64* @param b the buffer into which the data is read65* @return the actual number of bytes read, or -1 if the end of the log66* or end of the stream is reached.67* @exception IOException If an I/O error has occurred.68*/69public int read(byte b[]) throws IOException {70return read(b, 0, b.length);71}7273/**74* Reads data into an array of bytes.75* This method blocks until some input is available.76* @param b the buffer into which the data is read77* @param off the start offset of the data78* @param len the maximum number of bytes read79* @return the actual number of bytes read, or -1 if the end of the log or80* end of the stream is reached.81* @exception IOException If an I/O error has occurred.82*/83public int read(byte b[], int off, int len) throws IOException {84if (length == 0)85return -1;86len = (length < len) ? length : len;87int n = in.read(b, off, len);88length = (n != -1) ? length - n : 0;89return n;90}9192/**93* Skips n bytes of input.94* @param n the number of bytes to be skipped95* @return the actual number of bytes skipped.96* @exception IOException If an I/O error has occurred.97*/98public long skip(long n) throws IOException {99if (n > Integer.MAX_VALUE)100throw new IOException("Too many bytes to skip - " + n);101if (length == 0)102return 0;103n = (length < n) ? length : n;104n = in.skip(n);105length -= n;106return n;107}108109/**110* Returns the number of bytes that can be read without blocking.111* @return the number of available bytes, which is initially112* equal to the file size.113*/114public int available() throws IOException {115int avail = in.available();116return (length < avail) ? length : avail;117}118119/**120* Closes the input stream. No further input can be read.121* the stream.122*/123public void close() {124length = 0;125}126127/**128* Closes the stream when garbage is collected.129*/130protected void finalize() throws IOException {131close();132}133}134135136