History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/ssl/AppInputStream.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/ssl/AppInputStream.java+150−0
@@ -0,0 +1,150 @@
1/*
2 * Copyright (c) 1996, 2009, 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
26
27package sun.security.ssl;
28
29import java.io.*;
30
31/**
32 * InputStream for application data as returned by SSLSocket.getInputStream().
33 * It uses an InputRecord as internal buffer that is refilled on demand
34 * whenever it runs out of data.
35 *
36 * @author David Brownell
37 */
38class AppInputStream extends InputStream {
39
40 // static dummy array we use to implement skip()
41 private final static byte[] SKIP_ARRAY = new byte[1024];
42
43 private SSLSocketImpl c;
44 InputRecord r;
45
46 // One element array used to implement the single byte read() method
47 private final byte[] oneByte = new byte[1];
48
49 AppInputStream(SSLSocketImpl conn) {
50 r = new InputRecord();
51 c = conn;
52 }
53
54 /**
55 * Return the minimum number of bytes that can be read without blocking.
56 * Currently not synchronized.
57 */
58 public int available() throws IOException {
59 if (c.checkEOF() || (r.isAppDataValid() == false)) {
60 return 0;
61 }
62 return r.available();
63 }
64
65 /**
66 * Read a single byte, returning -1 on non-fault EOF status.
67 */
68 public synchronized int read() throws IOException {
69 int n = read(oneByte, 0, 1);
70 if (n <= 0) { // EOF
71 return -1;
72 }
73 return oneByte[0] & 0xff;
74 }
75
76 /**
77 * Read up to "len" bytes into this buffer, starting at "off".
78 * If the layer above needs more data, it asks for more, so we
79 * are responsible only for blocking to fill at most one buffer,
80 * and returning "-1" on non-fault EOF status.
81 */
82 public synchronized int read(byte b[], int off, int len)
83 throws IOException {
84 if (b == null) {
85 throw new NullPointerException();
86 } else if (off < 0 || len < 0 || len > b.length - off) {
87 throw new IndexOutOfBoundsException();
88 } else if (len == 0) {
89 return 0;
90 }
91
92 if (c.checkEOF()) {
93 return -1;
94 }
95 try {
96 /*
97 * Read data if needed ... notice that the connection guarantees
98 * that handshake, alert, and change cipher spec data streams are
99 * handled as they arrive, so we never see them here.
100 */
101 while (r.available() == 0) {
102 c.readDataRecord(r);
103 if (c.checkEOF()) {
104 return -1;
105 }
106 }
107
108 int howmany = Math.min(len, r.available());
109 howmany = r.read(b, off, howmany);
110 return howmany;
111 } catch (Exception e) {
112 // shutdown and rethrow (wrapped) exception as appropriate
113 c.handleException(e);
114 // dummy for compiler
115 return -1;
116 }
117 }
118
119
120 /**
121 * Skip n bytes. This implementation is somewhat less efficient
122 * than possible, but not badly so (redundant copy). We reuse
123 * the read() code to keep things simpler. Note that SKIP_ARRAY
124 * is static and may garbled by concurrent use, but we are not interested
125 * in the data anyway.
126 */
127 public synchronized long skip(long n) throws IOException {
128 long skipped = 0;
129 while (n > 0) {
130 int len = (int)Math.min(n, SKIP_ARRAY.length);
131 int r = read(SKIP_ARRAY, 0, len);
132 if (r <= 0) {
133 break;
134 }
135 n -= r;
136 skipped += r;
137 }
138 return skipped;
139 }
140
141 /*
142 * Socket close is already synchronized, no need to block here.
143 */
144 public void close() throws IOException {
145 c.close();
146 }
147
148 // inherit default mark/reset behavior (throw Exceptions) from InputStream
149
150}