1 /* RecordOutputStream.java -- record layer output.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.net.ssl.provider;
41 import gnu.classpath.SystemProperties;
42 import gnu.classpath.debug.Component;
43 import gnu.classpath.debug.SystemLogger;
45 import java.io.FilterOutputStream;
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.io.PrintWriter;
50 import java.util.logging.Logger;
53 * An output stream for writing data to the record layer. All data written
54 * to this stream (through any of the write methods) is immediately sent
55 * as a full record, so it is advisable to write large arrays to the stream
56 * instead of one byte at a time (alternatively, a {@link
57 * java.io.BufferedOutputStream} can be used).
59 class RecordOutputStream extends FilterOutputStream
63 // -------------------------------------------------------------------------
65 private static final boolean DEBUG_RECORD_LAYER = true;
66 private static final Logger logger = SystemLogger.SYSTEM;
69 * The content type of this output stream.
71 private final ContentType type;
74 * The security parameters.
76 private final SecurityParameters params;
78 private final boolean emitEmpty;
80 private static final byte[] ZERO = new byte[0];
83 // -------------------------------------------------------------------------
85 RecordOutputStream (final OutputStream out, final ContentType type,
86 final SecurityParameters params)
91 String empty = Util.getSecurityProperty ("jessie.emit.empty.records");
94 // IE panics if it gets an empty record; so, leave this false
98 emitEmpty = Boolean.valueOf (empty).booleanValue () &&
99 type == ContentType.APPLICATION_DATA;
103 // -------------------------------------------------------------------------
105 public void write (int b) throws IOException
107 write (new byte[] { (byte) b });
110 public void write (byte[] buf) throws IOException
112 write (buf, 0, buf.length);
115 public void write (byte[] buf, int off, int len) throws IOException
117 if (off < 0 || len < 0 || off + len > buf.length)
119 throw new ArrayIndexOutOfBoundsException ("size=" + buf.length +
120 " off=" + off + " len=" + len);
129 byte[] fragment = params.encrypt (ZERO, 0, 0, type);
130 if (DEBUG_RECORD_LAYER)
132 logger.log (Component.SSL_RECORD_LAYER,
133 ">> WRITING RECORD <<{4}" +
136 " version = {1};{4}" +
137 " length = {2};{4}" +
139 "} TLSCiphertext;", new Object[]
141 type, params.getVersion (), new Integer (fragment.length),
142 Util.hexDump (fragment, " "),
143 SystemProperties.getProperty ("line.separator")
146 out.write (type.getValue());
147 params.getVersion().write (out);
148 out.write ((fragment.length >>> 8) & 0xFF);
149 out.write ( fragment.length & 0xFF);
150 out.write (fragment);
153 len2 = Math.min (len - count, params.getFragmentLength());
154 if (DEBUG_RECORD_LAYER)
156 logger.log (Component.SSL_RECORD_LAYER,
157 "writing chunk size={0}", new Integer (len2));
161 byte[] fragment = params.encrypt (buf, off + count, len2, type);
162 if (DEBUG_RECORD_LAYER)
164 logger.log (Component.SSL_RECORD_LAYER,
165 ">> WRITING RECORD <<{4}" +
168 " version = {1};{4}" +
169 " length = {2};{4}" +
171 "} TLSCiphertext;", new Object[]
173 type, params.getVersion (), new Integer (fragment.length),
174 Util.hexDump (fragment, " "),
175 SystemProperties.getProperty ("line.separator")
178 out.write (type.getValue());
179 params.getVersion().write (out);
180 out.write ((fragment.length >>> 8) & 0xFF);
181 out.write ( fragment.length & 0xFF);
182 out.write (fragment);