KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > EXTDTAReaderInputStream


1 /*
2    Derby - Class org.apache.derby.impl.drda.EXTDTAReaderInputStream
3
4    Licensed to the Apache Software Foundation (ASF) under one
5    or more contributor license agreements. See the NOTICE file
6    distributed with this work for additional information
7    regarding copyright ownership. The ASF licenses this file
8    to you under the Apache License, Version 2.0 (the
9    "License"); you may not use this file except in compliance
10    with the License. You may obtain a copy of the License at
11    
12    http://www.apache.org/licenses/LICENSE-2.0
13    
14    Unless required by applicable law or agreed to in writing,
15    software distributed under the License is distributed on an
16    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17    KIND, either express or implied. See the License for the
18    specific language governing permissions and limitations
19    under the License.
20 */

21 package org.apache.derby.impl.drda;
22 import java.io.InputStream JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * Implementation of InputStream which get EXTDTA from the DDMReader.
28  * This class can be used to stream LOBs from Network client to the
29  * Network server.
30  */

31 final class EXTDTAReaderInputStream extends InputStream JavaDoc
32 {
33     /**
34      * Constructor
35      * @param reader The reader to get data from
36      * @exception DRDAProtocolException if thrown while initializing current
37      * buffer.
38      */

39     EXTDTAReaderInputStream(final DDMReader reader)
40         throws DRDAProtocolException
41     {
42         super();
43         this.reader = reader;
44         this.length = reader.getDdmLength();
45         this.remainingBytes = length;
46         this.currentBuffer =
47             reader.readLOBInitStream(remainingBytes);
48     }
49
50     /**
51      * Reads the next byte of data from the input stream.
52      *
53      * <p> This subclass of InputStream implements this method by reading
54      * the next byte from the current buffer. If there is more data,
55      * it will be requested a new buffer from the DDMReader.
56      *
57      * @return the next byte of data, or <code>-1</code> if the end of the
58      * stream is reached.
59      * @exception IOException if an I/O error occurs.
60      * @see java.io.InputStream#read()
61      */

62     public final int read()
63         throws IOException JavaDoc
64     {
65         if (remainingBytes <= 0) {
66             return -1;
67         }
68         int val = (currentBuffer == null) ? -1 : currentBuffer.read();
69         if (val < 0) {
70             val = refreshCurrentBuffer();
71         }
72         remainingBytes--;
73         return val;
74     }
75     
76     /**
77      * Reads up to <code>len</code> bytes of data from the input stream into
78      * an array of bytes. An attempt is made to read as many as
79      * <code>len</code> bytes, but a smaller number may be read, possibly
80      * zero. The number of bytes actually read is returned as an integer.
81      *
82      * This subclass implements this method by calling this method on the
83      * current buffer, which is an instance of ByteArrayInputStream. If the
84      * current buffer does not have any data, it will be requested a new
85      * buffer from the DDMReader.
86      *
87      * @param b the buffer into which the data is read.
88      * @param off the start offset in array <code>b</code>
89      * at which the data is written.
90      * @param len the maximum number of bytes to read.
91      * @return the total number of bytes read into the buffer, or
92      * <code>-1</code> if there is no more data because the end of
93      * the stream has been reached.
94      * @exception IOException if an I/O error occurs.
95      * @exception NullPointerException if <code>b</code> is <code>null</code>.
96      * @see java.io.InputStream#read(byte[], int, int)
97      */

98     public final int read(final byte[] b,
99                           final int off,
100                           final int len)
101         throws IOException JavaDoc
102     {
103         if (remainingBytes <= 0) {
104             return -1;
105         }
106         int val = currentBuffer.read(b, off, len);
107         if (val < 0) {
108             currentBuffer =
109                 reader.readLOBContinuationStream(remainingBytes);
110             val = currentBuffer.read(b, off, len);
111         }
112         remainingBytes -= val;
113         return val;
114     }
115
116     /**
117      * Returns the number of bytes that can be read (or skipped over) from
118      * this input stream without blocking by the next caller of a method for
119      * this input stream.
120      *
121      * <p> This subclass implements this method by calling available on
122      * the current buffer, which is a ByteInputStreamReader.
123      *
124      * @return the number of bytes that can be read from this input stream
125      * without blocking.
126      */

127     public final int available()
128     {
129         if (remainingBytes <= 0) {
130             return 0;
131         }
132         return currentBuffer.available();
133     }
134
135     /**
136      * Return the length if this stream. The length includes data which has
137      * been read.
138      * @return length of this stream.
139      */

140     final long getLength()
141     {
142         return length;
143     }
144     
145     /**
146      * Refresh the current buffer from the DDMReader
147      * @exception IOException if there is a IOException when
148      * refreshing the buffer from DDMReader
149      * @return the next byte of data, or <code>-1</code> if the end of the
150      * stream is reached.
151      */

152     private int refreshCurrentBuffer()
153         throws IOException JavaDoc
154     {
155         if (remainingBytes > 0) {
156             currentBuffer =
157                 reader.readLOBContinuationStream(remainingBytes);
158             return currentBuffer.read();
159         } else {
160             return -1;
161         }
162     }
163     
164     /** Length of stream */
165     private final long length;
166     
167     /** DDMReader. Used to get more data. */
168     private final DDMReader reader;
169     
170     /** Remaining bytes in stream */
171     private long remainingBytes;
172     
173     /** Current data buffer */
174     private ByteArrayInputStream JavaDoc currentBuffer;
175
176 }
177
Popular Tags