src/java.base/share/classes/java/io/OutputStream.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 48461 6a1c3a5e04f3
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    31  * This abstract class is the superclass of all classes representing
    31  * This abstract class is the superclass of all classes representing
    32  * an output stream of bytes. An output stream accepts output bytes
    32  * an output stream of bytes. An output stream accepts output bytes
    33  * and sends them to some sink.
    33  * and sends them to some sink.
    34  * <p>
    34  * <p>
    35  * Applications that need to define a subclass of
    35  * Applications that need to define a subclass of
    36  * <code>OutputStream</code> must always provide at least a method
    36  * {@code OutputStream} must always provide at least a method
    37  * that writes one byte of output.
    37  * that writes one byte of output.
    38  *
    38  *
    39  * @author  Arthur van Hoff
    39  * @author  Arthur van Hoff
    40  * @see     java.io.BufferedOutputStream
    40  * @see     java.io.BufferedOutputStream
    41  * @see     java.io.ByteArrayOutputStream
    41  * @see     java.io.ByteArrayOutputStream
    45  * @see     java.io.OutputStream#write(int)
    45  * @see     java.io.OutputStream#write(int)
    46  * @since   1.0
    46  * @since   1.0
    47  */
    47  */
    48 public abstract class OutputStream implements Closeable, Flushable {
    48 public abstract class OutputStream implements Closeable, Flushable {
    49     /**
    49     /**
       
    50      * Constructor for subclasses to call.
       
    51      */
       
    52     public OutputStream() {}
       
    53 
       
    54     /**
    50      * Returns a new {@code OutputStream} which discards all bytes.  The
    55      * Returns a new {@code OutputStream} which discards all bytes.  The
    51      * returned stream is initially open.  The stream is closed by calling
    56      * returned stream is initially open.  The stream is closed by calling
    52      * the {@code close()} method.  Subsequent calls to {@code close()} have
    57      * the {@code close()} method.  Subsequent calls to {@code close()} have
    53      * no effect.
    58      * no effect.
    54      *
    59      *
    91         };
    96         };
    92     }
    97     }
    93 
    98 
    94     /**
    99     /**
    95      * Writes the specified byte to this output stream. The general
   100      * Writes the specified byte to this output stream. The general
    96      * contract for <code>write</code> is that one byte is written
   101      * contract for {@code write} is that one byte is written
    97      * to the output stream. The byte to be written is the eight
   102      * to the output stream. The byte to be written is the eight
    98      * low-order bits of the argument <code>b</code>. The 24
   103      * low-order bits of the argument {@code b}. The 24
    99      * high-order bits of <code>b</code> are ignored.
   104      * high-order bits of {@code b} are ignored.
   100      * <p>
   105      * <p>
   101      * Subclasses of <code>OutputStream</code> must provide an
   106      * Subclasses of {@code OutputStream} must provide an
   102      * implementation for this method.
   107      * implementation for this method.
   103      *
   108      *
   104      * @param      b   the <code>byte</code>.
   109      * @param      b   the {@code byte}.
   105      * @exception  IOException  if an I/O error occurs. In particular,
   110      * @throws     IOException  if an I/O error occurs. In particular,
   106      *             an <code>IOException</code> may be thrown if the
   111      *             an {@code IOException} may be thrown if the
   107      *             output stream has been closed.
   112      *             output stream has been closed.
   108      */
   113      */
   109     public abstract void write(int b) throws IOException;
   114     public abstract void write(int b) throws IOException;
   110 
   115 
   111     /**
   116     /**
   112      * Writes <code>b.length</code> bytes from the specified byte array
   117      * Writes {@code b.length} bytes from the specified byte array
   113      * to this output stream. The general contract for <code>write(b)</code>
   118      * to this output stream. The general contract for {@code write(b)}
   114      * is that it should have exactly the same effect as the call
   119      * is that it should have exactly the same effect as the call
   115      * <code>write(b, 0, b.length)</code>.
   120      * {@code write(b, 0, b.length)}.
   116      *
   121      *
   117      * @param      b   the data.
   122      * @param      b   the data.
   118      * @exception  IOException  if an I/O error occurs.
   123      * @throws     IOException  if an I/O error occurs.
   119      * @see        java.io.OutputStream#write(byte[], int, int)
   124      * @see        java.io.OutputStream#write(byte[], int, int)
   120      */
   125      */
   121     public void write(byte b[]) throws IOException {
   126     public void write(byte b[]) throws IOException {
   122         write(b, 0, b.length);
   127         write(b, 0, b.length);
   123     }
   128     }
   124 
   129 
   125     /**
   130     /**
   126      * Writes <code>len</code> bytes from the specified byte array
   131      * Writes {@code len} bytes from the specified byte array
   127      * starting at offset <code>off</code> to this output stream.
   132      * starting at offset {@code off} to this output stream.
   128      * The general contract for <code>write(b, off, len)</code> is that
   133      * The general contract for {@code write(b, off, len)} is that
   129      * some of the bytes in the array <code>b</code> are written to the
   134      * some of the bytes in the array {@code b} are written to the
   130      * output stream in order; element <code>b[off]</code> is the first
   135      * output stream in order; element {@code b[off]} is the first
   131      * byte written and <code>b[off+len-1]</code> is the last byte written
   136      * byte written and {@code b[off+len-1]} is the last byte written
   132      * by this operation.
   137      * by this operation.
   133      * <p>
   138      * <p>
   134      * The <code>write</code> method of <code>OutputStream</code> calls
   139      * The {@code write} method of {@code OutputStream} calls
   135      * the write method of one argument on each of the bytes to be
   140      * the write method of one argument on each of the bytes to be
   136      * written out. Subclasses are encouraged to override this method and
   141      * written out. Subclasses are encouraged to override this method and
   137      * provide a more efficient implementation.
   142      * provide a more efficient implementation.
   138      * <p>
   143      * <p>
   139      * If <code>b</code> is <code>null</code>, a
   144      * If {@code b} is {@code null}, a
   140      * <code>NullPointerException</code> is thrown.
   145      * {@code NullPointerException} is thrown.
   141      * <p>
   146      * <p>
   142      * If <code>off</code> is negative, or <code>len</code> is negative, or
   147      * If {@code off} is negative, or {@code len} is negative, or
   143      * <code>off+len</code> is greater than the length of the array
   148      * {@code off+len} is greater than the length of the array
   144      * {@code b}, then an {@code IndexOutOfBoundsException} is thrown.
   149      * {@code b}, then an {@code IndexOutOfBoundsException} is thrown.
   145      *
   150      *
   146      * @param      b     the data.
   151      * @param      b     the data.
   147      * @param      off   the start offset in the data.
   152      * @param      off   the start offset in the data.
   148      * @param      len   the number of bytes to write.
   153      * @param      len   the number of bytes to write.
   149      * @exception  IOException  if an I/O error occurs. In particular,
   154      * @throws     IOException  if an I/O error occurs. In particular,
   150      *             an <code>IOException</code> is thrown if the output
   155      *             an {@code IOException} is thrown if the output
   151      *             stream is closed.
   156      *             stream is closed.
   152      */
   157      */
   153     public void write(byte b[], int off, int len) throws IOException {
   158     public void write(byte b[], int off, int len) throws IOException {
   154         Objects.checkFromIndexSize(off, len, b.length);
   159         Objects.checkFromIndexSize(off, len, b.length);
   155         // len == 0 condition implicitly handled by loop bounds
   160         // len == 0 condition implicitly handled by loop bounds
   158         }
   163         }
   159     }
   164     }
   160 
   165 
   161     /**
   166     /**
   162      * Flushes this output stream and forces any buffered output bytes
   167      * Flushes this output stream and forces any buffered output bytes
   163      * to be written out. The general contract of <code>flush</code> is
   168      * to be written out. The general contract of {@code flush} is
   164      * that calling it is an indication that, if any bytes previously
   169      * that calling it is an indication that, if any bytes previously
   165      * written have been buffered by the implementation of the output
   170      * written have been buffered by the implementation of the output
   166      * stream, such bytes should immediately be written to their
   171      * stream, such bytes should immediately be written to their
   167      * intended destination.
   172      * intended destination.
   168      * <p>
   173      * <p>
   170      * the underlying operating system, for example a file, then flushing the
   175      * the underlying operating system, for example a file, then flushing the
   171      * stream guarantees only that bytes previously written to the stream are
   176      * stream guarantees only that bytes previously written to the stream are
   172      * passed to the operating system for writing; it does not guarantee that
   177      * passed to the operating system for writing; it does not guarantee that
   173      * they are actually written to a physical device such as a disk drive.
   178      * they are actually written to a physical device such as a disk drive.
   174      * <p>
   179      * <p>
   175      * The <code>flush</code> method of <code>OutputStream</code> does nothing.
   180      * The {@code flush} method of {@code OutputStream} does nothing.
   176      *
   181      *
   177      * @exception  IOException  if an I/O error occurs.
   182      * @throws     IOException  if an I/O error occurs.
   178      */
   183      */
   179     public void flush() throws IOException {
   184     public void flush() throws IOException {
   180     }
   185     }
   181 
   186 
   182     /**
   187     /**
   183      * Closes this output stream and releases any system resources
   188      * Closes this output stream and releases any system resources
   184      * associated with this stream. The general contract of <code>close</code>
   189      * associated with this stream. The general contract of {@code close}
   185      * is that it closes the output stream. A closed stream cannot perform
   190      * is that it closes the output stream. A closed stream cannot perform
   186      * output operations and cannot be reopened.
   191      * output operations and cannot be reopened.
   187      * <p>
   192      * <p>
   188      * The <code>close</code> method of <code>OutputStream</code> does nothing.
   193      * The {@code close} method of {@code OutputStream} does nothing.
   189      *
   194      *
   190      * @exception  IOException  if an I/O error occurs.
   195      * @throws     IOException  if an I/O error occurs.
   191      */
   196      */
   192     public void close() throws IOException {
   197     public void close() throws IOException {
   193     }
   198     }
   194 
   199 
   195 }
   200 }