rt/emul/compact/src/main/java/java/io/OutputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 601 emul/compact/src/main/java/java/io/OutputStream.java@5198affdb915
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@601
     1
/*
jaroslav@601
     2
 * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
jaroslav@601
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@601
     4
 *
jaroslav@601
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@601
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@601
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@601
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@601
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@601
    10
 *
jaroslav@601
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@601
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@601
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@601
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@601
    15
 * accompanied this code).
jaroslav@601
    16
 *
jaroslav@601
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@601
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@601
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@601
    20
 *
jaroslav@601
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@601
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@601
    23
 * questions.
jaroslav@601
    24
 */
jaroslav@601
    25
jaroslav@601
    26
package java.io;
jaroslav@601
    27
jaroslav@601
    28
/**
jaroslav@601
    29
 * This abstract class is the superclass of all classes representing
jaroslav@601
    30
 * an output stream of bytes. An output stream accepts output bytes
jaroslav@601
    31
 * and sends them to some sink.
jaroslav@601
    32
 * <p>
jaroslav@601
    33
 * Applications that need to define a subclass of
jaroslav@601
    34
 * <code>OutputStream</code> must always provide at least a method
jaroslav@601
    35
 * that writes one byte of output.
jaroslav@601
    36
 *
jaroslav@601
    37
 * @author  Arthur van Hoff
jaroslav@601
    38
 * @see     java.io.BufferedOutputStream
jaroslav@601
    39
 * @see     java.io.ByteArrayOutputStream
jaroslav@601
    40
 * @see     java.io.DataOutputStream
jaroslav@601
    41
 * @see     java.io.FilterOutputStream
jaroslav@601
    42
 * @see     java.io.InputStream
jaroslav@601
    43
 * @see     java.io.OutputStream#write(int)
jaroslav@601
    44
 * @since   JDK1.0
jaroslav@601
    45
 */
jaroslav@601
    46
public abstract class OutputStream implements Closeable, Flushable {
jaroslav@601
    47
    /**
jaroslav@601
    48
     * Writes the specified byte to this output stream. The general
jaroslav@601
    49
     * contract for <code>write</code> is that one byte is written
jaroslav@601
    50
     * to the output stream. The byte to be written is the eight
jaroslav@601
    51
     * low-order bits of the argument <code>b</code>. The 24
jaroslav@601
    52
     * high-order bits of <code>b</code> are ignored.
jaroslav@601
    53
     * <p>
jaroslav@601
    54
     * Subclasses of <code>OutputStream</code> must provide an
jaroslav@601
    55
     * implementation for this method.
jaroslav@601
    56
     *
jaroslav@601
    57
     * @param      b   the <code>byte</code>.
jaroslav@601
    58
     * @exception  IOException  if an I/O error occurs. In particular,
jaroslav@601
    59
     *             an <code>IOException</code> may be thrown if the
jaroslav@601
    60
     *             output stream has been closed.
jaroslav@601
    61
     */
jaroslav@601
    62
    public abstract void write(int b) throws IOException;
jaroslav@601
    63
jaroslav@601
    64
    /**
jaroslav@601
    65
     * Writes <code>b.length</code> bytes from the specified byte array
jaroslav@601
    66
     * to this output stream. The general contract for <code>write(b)</code>
jaroslav@601
    67
     * is that it should have exactly the same effect as the call
jaroslav@601
    68
     * <code>write(b, 0, b.length)</code>.
jaroslav@601
    69
     *
jaroslav@601
    70
     * @param      b   the data.
jaroslav@601
    71
     * @exception  IOException  if an I/O error occurs.
jaroslav@601
    72
     * @see        java.io.OutputStream#write(byte[], int, int)
jaroslav@601
    73
     */
jaroslav@601
    74
    public void write(byte b[]) throws IOException {
jaroslav@601
    75
        write(b, 0, b.length);
jaroslav@601
    76
    }
jaroslav@601
    77
jaroslav@601
    78
    /**
jaroslav@601
    79
     * Writes <code>len</code> bytes from the specified byte array
jaroslav@601
    80
     * starting at offset <code>off</code> to this output stream.
jaroslav@601
    81
     * The general contract for <code>write(b, off, len)</code> is that
jaroslav@601
    82
     * some of the bytes in the array <code>b</code> are written to the
jaroslav@601
    83
     * output stream in order; element <code>b[off]</code> is the first
jaroslav@601
    84
     * byte written and <code>b[off+len-1]</code> is the last byte written
jaroslav@601
    85
     * by this operation.
jaroslav@601
    86
     * <p>
jaroslav@601
    87
     * The <code>write</code> method of <code>OutputStream</code> calls
jaroslav@601
    88
     * the write method of one argument on each of the bytes to be
jaroslav@601
    89
     * written out. Subclasses are encouraged to override this method and
jaroslav@601
    90
     * provide a more efficient implementation.
jaroslav@601
    91
     * <p>
jaroslav@601
    92
     * If <code>b</code> is <code>null</code>, a
jaroslav@601
    93
     * <code>NullPointerException</code> is thrown.
jaroslav@601
    94
     * <p>
jaroslav@601
    95
     * If <code>off</code> is negative, or <code>len</code> is negative, or
jaroslav@601
    96
     * <code>off+len</code> is greater than the length of the array
jaroslav@601
    97
     * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
jaroslav@601
    98
     *
jaroslav@601
    99
     * @param      b     the data.
jaroslav@601
   100
     * @param      off   the start offset in the data.
jaroslav@601
   101
     * @param      len   the number of bytes to write.
jaroslav@601
   102
     * @exception  IOException  if an I/O error occurs. In particular,
jaroslav@601
   103
     *             an <code>IOException</code> is thrown if the output
jaroslav@601
   104
     *             stream is closed.
jaroslav@601
   105
     */
jaroslav@601
   106
    public void write(byte b[], int off, int len) throws IOException {
jaroslav@601
   107
        if (b == null) {
jaroslav@601
   108
            throw new NullPointerException();
jaroslav@601
   109
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
jaroslav@601
   110
                   ((off + len) > b.length) || ((off + len) < 0)) {
jaroslav@601
   111
            throw new IndexOutOfBoundsException();
jaroslav@601
   112
        } else if (len == 0) {
jaroslav@601
   113
            return;
jaroslav@601
   114
        }
jaroslav@601
   115
        for (int i = 0 ; i < len ; i++) {
jaroslav@601
   116
            write(b[off + i]);
jaroslav@601
   117
        }
jaroslav@601
   118
    }
jaroslav@601
   119
jaroslav@601
   120
    /**
jaroslav@601
   121
     * Flushes this output stream and forces any buffered output bytes
jaroslav@601
   122
     * to be written out. The general contract of <code>flush</code> is
jaroslav@601
   123
     * that calling it is an indication that, if any bytes previously
jaroslav@601
   124
     * written have been buffered by the implementation of the output
jaroslav@601
   125
     * stream, such bytes should immediately be written to their
jaroslav@601
   126
     * intended destination.
jaroslav@601
   127
     * <p>
jaroslav@601
   128
     * If the intended destination of this stream is an abstraction provided by
jaroslav@601
   129
     * the underlying operating system, for example a file, then flushing the
jaroslav@601
   130
     * stream guarantees only that bytes previously written to the stream are
jaroslav@601
   131
     * passed to the operating system for writing; it does not guarantee that
jaroslav@601
   132
     * they are actually written to a physical device such as a disk drive.
jaroslav@601
   133
     * <p>
jaroslav@601
   134
     * The <code>flush</code> method of <code>OutputStream</code> does nothing.
jaroslav@601
   135
     *
jaroslav@601
   136
     * @exception  IOException  if an I/O error occurs.
jaroslav@601
   137
     */
jaroslav@601
   138
    public void flush() throws IOException {
jaroslav@601
   139
    }
jaroslav@601
   140
jaroslav@601
   141
    /**
jaroslav@601
   142
     * Closes this output stream and releases any system resources
jaroslav@601
   143
     * associated with this stream. The general contract of <code>close</code>
jaroslav@601
   144
     * is that it closes the output stream. A closed stream cannot perform
jaroslav@601
   145
     * output operations and cannot be reopened.
jaroslav@601
   146
     * <p>
jaroslav@601
   147
     * The <code>close</code> method of <code>OutputStream</code> does nothing.
jaroslav@601
   148
     *
jaroslav@601
   149
     * @exception  IOException  if an I/O error occurs.
jaroslav@601
   150
     */
jaroslav@601
   151
    public void close() throws IOException {
jaroslav@601
   152
    }
jaroslav@601
   153
jaroslav@601
   154
}