01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Alexander Y. Kleymenov
20: * @version $Revision$
21: */package org.apache.harmony.xnet.provider.jsse;
22:
23: import org.apache.harmony.xnet.provider.jsse.SSLInputStream;
24:
25: import java.io.IOException;
26: import java.nio.ByteBuffer;
27:
28: /**
29: * This is a wrapper input stream for ByteBuffer data source.
30: * Among with the read functionality it provides info
31: * about number of cunsumed bytes from the source ByteBuffer.
32: * The source ByteBuffer object can be reseted.
33: * So one instance of this wrapper can be reused for several
34: * ByteBuffer data sources.
35: */
36: public class SSLBufferedInput extends SSLInputStream {
37:
38: private ByteBuffer in;
39: private int bytik;
40: private int consumed = 0;
41:
42: /**
43: * Constructor
44: */
45: protected SSLBufferedInput() {
46: }
47:
48: /**
49: * Sets the buffer as a data source
50: */
51: protected void setSourceBuffer(ByteBuffer in) {
52: consumed = 0;
53: this .in = in;
54: }
55:
56: /**
57: * Returns the number of bytes available for reading.
58: */
59: public int available() throws IOException {
60: // in assumption that the buffer has been set
61: return in.remaining();
62: }
63:
64: /**
65: * Returns the number of consumed bytes.
66: */
67: protected int consumed() {
68: return consumed;
69: }
70:
71: /**
72: * Reads the following byte value. If there are no bytes in the source
73: * buffer, method throws java.nio.BufferUnderflowException.
74: */
75: public int read() throws IOException {
76: // TODO: implement optimized read(int)
77: // and read(byte[], int, int) methods
78: bytik = in.get() & 0x00FF;
79: consumed++;
80: return bytik;
81: }
82: }
|