OSDN Git Service

* config/i386/i386.md (*sinxf2): Rename to *sinxf2_i387.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / net / ssl / provider / RecordOutputStream.java
1 /* RecordOutputStream.java -- record layer output.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3
4 This file is a part of GNU Classpath.
5
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.
10
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.
15
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
19 USA
20
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
24 combination.
25
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.  */
37
38
39 package gnu.javax.net.ssl.provider;
40
41 import gnu.classpath.SystemProperties;
42 import gnu.classpath.debug.Component;
43 import gnu.classpath.debug.SystemLogger;
44
45 import java.io.FilterOutputStream;
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.io.PrintWriter;
49
50 import java.util.logging.Logger;
51
52 /**
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).
58  */
59 class RecordOutputStream extends FilterOutputStream
60 {
61
62   // Fields.
63   // -------------------------------------------------------------------------
64
65   private static final boolean DEBUG_RECORD_LAYER = true;
66   private static final Logger logger = SystemLogger.SYSTEM;
67
68   /**
69    * The content type of this output stream.
70    */
71   private final ContentType type;
72
73   /**
74    * The security parameters.
75    */
76   private final SecurityParameters params;
77
78   private final boolean emitEmpty;
79
80   private static final byte[] ZERO = new byte[0];
81
82   // Constructor.
83   // -------------------------------------------------------------------------
84
85   RecordOutputStream (final OutputStream out, final ContentType type,
86                       final SecurityParameters params)
87   {
88     super (out);
89     this.type = type;
90     this.params = params;
91     String empty = Util.getSecurityProperty ("jessie.emit.empty.records");
92     if (empty == null)
93       {
94         // IE panics if it gets an empty record; so, leave this false
95         // for the default.
96         empty = "false";
97       }
98     emitEmpty = Boolean.valueOf (empty).booleanValue () &&
99       type == ContentType.APPLICATION_DATA;
100   }
101
102   // Instance methods.
103   // -------------------------------------------------------------------------
104
105   public void write (int b) throws IOException
106   {
107     write (new byte[] { (byte) b });
108   }
109
110   public void write (byte[] buf) throws IOException
111   {
112     write (buf, 0, buf.length);
113   }
114
115   public void write (byte[] buf, int off, int len) throws IOException
116   {
117     if (off < 0 || len < 0 || off + len > buf.length)
118       {
119         throw new ArrayIndexOutOfBoundsException ("size=" + buf.length +
120                                                   " off=" + off + " len=" + len);
121       }
122
123     int count = 0;
124     int len2 = 0;
125     do
126       {
127         if (emitEmpty)
128           {
129             byte[] fragment = params.encrypt (ZERO, 0, 0, type);
130             if (DEBUG_RECORD_LAYER)
131               {
132                 logger.log (Component.SSL_RECORD_LAYER,
133                             ">> WRITING RECORD <<{4}" +
134                             "struct {{4}" +
135                             "  type = {0};{4}" +
136                             "  version = {1};{4}" +
137                             "  length = {2};{4}" +
138                             "{3}{4}" +
139                             "} TLSCiphertext;", new Object[]
140                   {
141                     type, params.getVersion (), new Integer (fragment.length),
142                     Util.hexDump (fragment, "  "),
143                     SystemProperties.getProperty ("line.separator")
144                   });
145               }
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);
151             out.flush ();
152           }
153         len2 = Math.min (len - count, params.getFragmentLength());
154         if (DEBUG_RECORD_LAYER)
155           {
156             logger.log (Component.SSL_RECORD_LAYER,
157                         "writing chunk size={0}", new Integer (len2));
158           }
159         synchronized (out)
160           {
161             byte[] fragment = params.encrypt (buf, off + count, len2, type);
162             if (DEBUG_RECORD_LAYER)
163               {
164                 logger.log (Component.SSL_RECORD_LAYER,
165                             ">> WRITING RECORD <<{4}" +
166                             "struct {{4}" +
167                             "  type = {0};{4}" +
168                             "  version = {1};{4}" +
169                             "  length = {2};{4}" +
170                             "{3}{4}" +
171                             "} TLSCiphertext;", new Object[]
172                   {
173                     type, params.getVersion (), new Integer (fragment.length),
174                     Util.hexDump (fragment, "  "),
175                     SystemProperties.getProperty ("line.separator")
176                   });
177               }
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);
183             out.flush ();
184           }
185         count += len2;
186       }
187     while (count < len);
188   }
189 }