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