1 21 package org.apache.derby.impl.drda; 22 import java.io.InputStream ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.IOException ; 25 26 31 final class EXTDTAReaderInputStream extends InputStream 32 { 33 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 62 public final int read() 63 throws IOException 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 98 public final int read(final byte[] b, 99 final int off, 100 final int len) 101 throws IOException 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 127 public final int available() 128 { 129 if (remainingBytes <= 0) { 130 return 0; 131 } 132 return currentBuffer.available(); 133 } 134 135 140 final long getLength() 141 { 142 return length; 143 } 144 145 152 private int refreshCurrentBuffer() 153 throws IOException 154 { 155 if (remainingBytes > 0) { 156 currentBuffer = 157 reader.readLOBContinuationStream(remainingBytes); 158 return currentBuffer.read(); 159 } else { 160 return -1; 161 } 162 } 163 164 165 private final long length; 166 167 168 private final DDMReader reader; 169 170 171 private long remainingBytes; 172 173 174 private ByteArrayInputStream currentBuffer; 175 176 } 177 | Popular Tags |