1 19 package com.mysql.jdbc; 20 21 import java.io.EOFException ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 import java.util.zip.DataFormatException ; 26 import java.util.zip.Inflater ; 27 28 29 37 class CompressedInputStream extends InputStream { 38 40 43 private Inflater inflater; 44 45 48 private InputStream in; 49 50 53 private byte[] uncompressedPacket; 54 55 58 private int pos = 0; 59 60 62 68 public CompressedInputStream(InputStream streamFromServer) { 69 this.in = streamFromServer; 70 this.inflater = new Inflater (); 71 } 72 73 75 78 public int available() throws IOException { 79 if (this.uncompressedPacket == null) { 80 return this.in.available(); 81 } 82 83 return this.uncompressedPacket.length - this.pos + this.in.available(); 84 } 85 86 89 public void close() throws IOException { 90 this.in.close(); 91 this.uncompressedPacket = null; 92 this.inflater = null; 93 } 94 95 98 public int read() throws IOException { 99 try { 100 getNextPacketIfRequired(1); 101 } catch (IOException ioEx) { 102 return -1; 103 } 104 105 return this.uncompressedPacket[this.pos++] & 0xff; 106 } 107 108 111 public int read(byte[] b, int off, int len) throws IOException { 112 if (b == null) { 113 throw new NullPointerException (); 114 } else if ((off < 0) || (off > b.length) || (len < 0) 115 || ((off + len) > b.length) || ((off + len) < 0)) { 116 throw new IndexOutOfBoundsException (); 117 } 118 119 if (len <= 0) { 120 return 0; 121 } 122 123 try { 124 getNextPacketIfRequired(len); 125 } catch (IOException ioEx) { 126 return -1; 127 } 128 129 System.arraycopy(this.uncompressedPacket, this.pos, b, off, len); 130 this.pos += len; 131 132 return len; 133 } 134 135 138 public int read(byte[] b) throws IOException { 139 return read(b, 0, b.length); 140 } 141 142 145 public long skip(long n) throws IOException { 146 long count = 0; 147 148 for (long i = 0; i < n; i++) { 149 int bytesRead = read(); 150 151 if (bytesRead == -1) { 152 break; 153 } 154 155 count++; 156 } 157 158 return count; 159 } 160 161 167 private void getNextPacketFromServer() throws IOException { 168 byte[] uncompressedBuffer = null; 169 170 int packetLength = this.in.read() + (this.in.read() << 8) 171 + (this.in.read() << 16); 172 173 if (packetLength == -65793) { 175 throw new IOException ("Unexpected end of input stream"); 176 } 177 178 this.in.read(); 180 181 int compressedLength = this.in.read() + (this.in.read() << 8) 182 + (this.in.read() << 16); 183 184 if (compressedLength > 0) { 185 uncompressedBuffer = new byte[compressedLength]; 186 187 byte[] compressedBuffer = new byte[packetLength]; 188 189 readFully(compressedBuffer, 0, packetLength); 190 191 try { 192 this.inflater.reset(); 193 } catch (NullPointerException npe) { 194 this.inflater = new Inflater (); 195 } 196 197 this.inflater.setInput(compressedBuffer); 198 199 try { 200 this.inflater.inflate(uncompressedBuffer); 201 } catch (DataFormatException dfe) { 202 throw new IOException ( 203 "Error while uncompressing packet from server."); 204 } 205 206 this.inflater.end(); 207 } else { 208 uncompressedBuffer = new byte[packetLength + 1]; 213 readFully(uncompressedBuffer, 0, packetLength); 214 } 215 216 if ((this.uncompressedPacket != null) 217 && (this.pos < this.uncompressedPacket.length)) { 218 int remainingLength = this.uncompressedPacket.length - this.pos; 219 220 byte[] combinedBuffer = new byte[remainingLength 221 + uncompressedBuffer.length]; 222 System.arraycopy(this.uncompressedPacket, this.pos, combinedBuffer, 0, 223 remainingLength); 224 System.arraycopy(uncompressedBuffer, 0, combinedBuffer, 225 remainingLength, uncompressedBuffer.length); 226 uncompressedBuffer = combinedBuffer; 227 } 228 229 this.uncompressedPacket = uncompressedBuffer; 230 this.pos = 0; 231 232 return; 233 } 234 235 242 private void getNextPacketIfRequired(int numBytes) 243 throws IOException { 244 if ((this.uncompressedPacket == null) 245 || ((this.pos + numBytes) > this.uncompressedPacket.length)) { 246 getNextPacketFromServer(); 247 } 248 } 249 250 private final int readFully(byte[] b, int off, int len) 251 throws IOException { 252 if (len < 0) { 253 throw new IndexOutOfBoundsException (); 254 } 255 256 int n = 0; 257 258 while (n < len) { 259 int count = this.in.read(b, off + n, len - n); 260 261 if (count < 0) { 262 throw new EOFException (); 263 } 264 265 n += count; 266 } 267 268 return n; 269 } 270 } 271 | Popular Tags |