1/*
2 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.ssl;
27
28import java.io.OutputStream;
29import java.io.IOException;
30import java.nio.ByteBuffer;
31
32/*
33 * OutputStream for application data as returned by SSLSocket.getOutputStream().
34 *
35 * @author  David Brownell
36 */
37class AppOutputStream extends OutputStream {
38
39    private SSLSocketImpl socket;
40
41    // One element array used to implement the write(byte) method
42    private final byte[] oneByte = new byte[1];
43
44    AppOutputStream(SSLSocketImpl conn) {
45        this.socket = conn;
46    }
47
48    /**
49     * Write the data out, NOW.
50     */
51    @Override
52    public synchronized void write(byte[] b, int off, int len)
53            throws IOException {
54        if (b == null) {
55            throw new NullPointerException();
56        } else if (off < 0 || len < 0 || len > b.length - off) {
57            throw new IndexOutOfBoundsException();
58        } else if (len == 0) {
59            return;
60        }
61
62        // check if the Socket is invalid (error or closed)
63        socket.checkWrite();
64
65        // Delegate the writing to the underlying socket.
66        try {
67            socket.writeRecord(b, off, len);
68            socket.checkWrite();
69        } catch (Exception e) {
70            // shutdown and rethrow (wrapped) exception as appropriate
71            socket.handleException(e);
72        }
73    }
74
75    /**
76     * Write one byte now.
77     */
78    @Override
79    public synchronized void write(int i) throws IOException {
80        oneByte[0] = (byte)i;
81        write(oneByte, 0, 1);
82    }
83
84    /*
85     * Socket close is already synchronized, no need to block here.
86     */
87    @Override
88    public void close() throws IOException {
89        socket.close();
90    }
91
92    // inherit no-op flush()
93}
94