0N/A/*
4918N/A * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage sun.security.ssl;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/*
0N/A * Output stream for application data. This is the kind of stream
0N/A * that's handed out via SSLSocket.getOutputStream(). It's all the application
0N/A * ever sees.
0N/A *
0N/A * Once the initial handshake has completed, application data may be
0N/A * interleaved with handshake data. That is handled internally and remains
0N/A * transparent to the application.
0N/A *
0N/A * @author David Brownell
0N/A */
0N/Aclass AppOutputStream extends OutputStream {
0N/A
0N/A private SSLSocketImpl c;
0N/A OutputRecord r;
0N/A
0N/A // One element array used to implement the write(byte) method
0N/A private final byte[] oneByte = new byte[1];
0N/A
0N/A AppOutputStream(SSLSocketImpl conn) {
0N/A r = new OutputRecord(Record.ct_application_data);
0N/A c = conn;
0N/A }
0N/A
0N/A /**
0N/A * Write the data out, NOW.
0N/A */
0N/A synchronized public void write(byte b[], int off, int len)
0N/A throws IOException {
897N/A if (b == null) {
897N/A throw new NullPointerException();
897N/A } else if (off < 0 || len < 0 || len > b.length - off) {
897N/A throw new IndexOutOfBoundsException();
897N/A } else if (len == 0) {
897N/A return;
897N/A }
897N/A
0N/A // check if the Socket is invalid (error or closed)
0N/A c.checkWrite();
897N/A
4466N/A /*
4466N/A * By default, we counter chosen plaintext issues on CBC mode
4466N/A * ciphersuites in SSLv3/TLS1.0 by sending one byte of application
4466N/A * data in the first record of every payload, and the rest in
4466N/A * subsequent record(s). Note that the issues have been solved in
4466N/A * TLS 1.1 or later.
4466N/A *
4466N/A * It is not necessary to split the very first application record of
4466N/A * a freshly negotiated TLS session, as there is no previous
4466N/A * application data to guess. To improve compatibility, we will not
4466N/A * split such records.
4466N/A *
4466N/A * This avoids issues in the outbound direction. For a full fix,
4466N/A * the peer must have similar protections.
4466N/A */
4466N/A boolean isFirstRecordOfThePayload = true;
4466N/A
0N/A // Always flush at the end of each application level record.
0N/A // This lets application synchronize read and write streams
0N/A // however they like; if we buffered here, they couldn't.
0N/A try {
0N/A do {
4918N/A boolean holdRecord = false;
4466N/A int howmuch;
4466N/A if (isFirstRecordOfThePayload && c.needToSplitPayload()) {
4466N/A howmuch = Math.min(0x01, r.availableDataBytes());
4918N/A /*
4918N/A * Nagle's algorithm (TCP_NODELAY) was coming into
4918N/A * play here when writing short (split) packets.
4918N/A * Signal to the OutputRecord code to internally
4918N/A * buffer this small packet until the next outbound
4918N/A * packet (of any type) is written.
4918N/A */
4918N/A if ((len != 1) && (howmuch == 1)) {
4918N/A holdRecord = true;
4918N/A }
4466N/A } else {
4466N/A howmuch = Math.min(len, r.availableDataBytes());
4466N/A }
4466N/A
4466N/A if (isFirstRecordOfThePayload && howmuch != 0) {
4466N/A isFirstRecordOfThePayload = false;
4466N/A }
0N/A
897N/A // NOTE: *must* call c.writeRecord() even for howmuch == 0
0N/A if (howmuch > 0) {
0N/A r.write(b, off, howmuch);
0N/A off += howmuch;
0N/A len -= howmuch;
0N/A }
4918N/A c.writeRecord(r, holdRecord);
0N/A c.checkWrite();
0N/A } while (len > 0);
0N/A } catch (Exception e) {
0N/A // shutdown and rethrow (wrapped) exception as appropriate
0N/A c.handleException(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Write one byte now.
0N/A */
0N/A synchronized public void write(int i) throws IOException {
0N/A oneByte[0] = (byte)i;
0N/A write(oneByte, 0, 1);
0N/A }
0N/A
0N/A /*
0N/A * Socket close is already synchronized, no need to block here.
0N/A */
0N/A public void close() throws IOException {
0N/A c.close();
0N/A }
0N/A
0N/A // inherit no-op flush()
0N/A}