OutputStream.java revision fdb2704414a9ed92394ada0d1395e4db86889465
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.io;
19
20import org.apache.harmony.luni.util.Msg;
21
22/**
23 * OutputStream is an abstract class for all byte output streams. It provides
24 * basic method implementations for writing bytes to a stream.
25 *
26 * @see InputStream
27 */
28public abstract class OutputStream implements Closeable, Flushable {
29
30    /**
31     * Default constructor.
32     */
33    public OutputStream() {
34        super();
35    }
36
37    /**
38     * Close this OutputStream. Concrete implementations of this class should
39     * free any resources during close. This implementation does nothing.
40     *
41     * @throws IOException
42     *             If an error occurs attempting to close this OutputStream.
43     */
44    public void close() throws IOException {
45        /* empty */
46    }
47
48    /**
49     * Flush this OutputStream. Concrete implementations of this class should
50     * ensure any pending writes to the underlying stream are written out when
51     * this method is envoked. This implementation does nothing.
52     *
53     * @throws IOException
54     *             If an error occurs attempting to flush this OutputStream.
55     */
56    public void flush() throws IOException {
57        /* empty */
58    }
59
60    /**
61     * Writes the entire contents of the byte array <code>buffer</code> to
62     * this OutputStream.
63     *
64     * @param buffer
65     *            the buffer to be written
66     *
67     * @throws IOException
68     *             If an error occurs attempting to write to this OutputStream.
69     */
70    public void write(byte buffer[]) throws IOException {
71        write(buffer, 0, buffer.length);
72    }
73
74    /**
75     * Writes <code>count</code> <code>bytes</code> from the byte array
76     * <code>buffer</code> starting at <code>offset</code> to this
77     * OutputStream.
78     *
79     * @param buffer
80     *            the buffer to be written
81     * @param offset
82     *            offset in buffer to get bytes
83     * @param count
84     *            number of bytes in buffer to write
85     *
86     * @throws IOException
87     *             If an error occurs attempting to write to this OutputStream.
88     * @throws IndexOutOfBoundsException
89     *             If offset or count are outside of bounds.
90     */
91    public void write(byte buffer[], int offset, int count) throws IOException {
92        // avoid int overflow, check null buffer
93        if (offset < 0 || offset > buffer.length || count < 0
94                || count > buffer.length - offset) {
95            throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
96        }
97        for (int i = offset; i < offset + count; i++) {
98            write(buffer[i]);
99        }
100    }
101
102    /**
103     * Writes the specified byte <code>oneByte</code> to this OutputStream.
104     * Only the low order byte of <code>oneByte</code> is written.
105     *
106     * @param oneByte
107     *            the byte to be written
108     *
109     * @throws IOException
110     *             If an error occurs attempting to write to this OutputStream.
111     */
112    public abstract void write(int oneByte) throws IOException;
113}
114