| 1 18 package com.izforge.izpack.io; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.zip.GZIPInputStream ; 25 26 import com.izforge.izpack.util.Debug; 27 28 34 public class FileSpanningInputStream extends InputStream  35 { 36 37 private static final int EOF = -1; 38 39 protected FileInputStream fileinputstream; 40 41 protected String volumename; 42 43 protected int currentvolumeindex; 44 45 protected int volumestotal; 46 47 protected static boolean nextvolumenotfound = false; 48 49 protected long filepointer; 50 51 protected GZIPInputStream zippedinputstream; 52 53 public FileSpanningInputStream(File volume, int volumestotal) throws IOException  54 { 55 fileinputstream = new FileInputStream (volume); 56 zippedinputstream = new GZIPInputStream (fileinputstream); 57 currentvolumeindex = 0; 58 volumename = volume.getAbsolutePath(); 59 this.volumestotal = volumestotal; 60 filepointer = 0; 61 Debug.trace("Opening stream to " + volume); 62 } 63 64 public FileSpanningInputStream(String volumename, int volumestotal) throws IOException  65 { 66 this(new File (volumename), volumestotal); 67 } 68 69 76 private boolean createInputStreamToNextVolume() throws IOException  77 { 78 currentvolumeindex++; 79 if (currentvolumeindex >= volumestotal) 81 { 82 Debug.error("last volume reached."); 83 return false; 84 } 85 String nextvolumename = volumename + "." + currentvolumeindex; 87 Debug.trace("Trying to use next volume: " + nextvolumename); 88 File nextvolumefile = new File (nextvolumename); 89 if (!nextvolumefile.exists()) 90 { 91 currentvolumeindex--; 92 nextvolumenotfound = true; 93 Debug.trace("volume not found"); 94 throw new VolumeNotFoundException(nextvolumename + "was not found.", nextvolumename); 95 } 96 Debug.trace("next volume found."); 97 fileinputstream = new FileInputStream (nextvolumefile); 99 zippedinputstream = new GZIPInputStream (fileinputstream); 100 nextvolumenotfound = false; 102 return true; 103 } 104 105 110 public int available() throws IOException  111 { 112 if (nextvolumenotfound) 113 { 114 createInputStreamToNextVolume(); 115 } 116 return zippedinputstream.available(); 118 } 119 120 125 public void close() throws IOException  126 { 127 zippedinputstream.close(); 128 fileinputstream.close(); 129 } 130 131 136 public int read() throws IOException  137 { 138 if (nextvolumenotfound) 139 { 140 createInputStreamToNextVolume(); 142 } 143 int nextbyte = zippedinputstream.read(); 144 filepointer++; 145 if (nextbyte == EOF) 146 { 147 try 150 { 151 zippedinputstream.close(); 152 } 153 catch (Exception e) 154 { 155 } 157 158 if (createInputStreamToNextVolume()) 159 { 160 nextbyte = zippedinputstream.read(); 162 filepointer++; 163 } 164 } 165 return nextbyte; 166 } 167 168 173 public int read(byte[] b, int off, int len) throws IOException  174 { 175 if (nextvolumenotfound) 176 { 177 createInputStreamToNextVolume(); 179 } 180 int bytesread = zippedinputstream.read(b, off, len); 181 filepointer += bytesread; 182 if (bytesread == EOF) 183 { 184 filepointer++; System.out.println("EOF reached."); 186 try 188 { 189 zippedinputstream.close(); 190 } 191 catch (Exception e) 192 { 193 } 195 if (createInputStreamToNextVolume()) 197 { 198 Debug.trace("next volume opened, continuing read"); 200 bytesread = zippedinputstream.read(b, off, len); 201 filepointer += bytesread; 202 } 204 } 205 return bytesread; 207 } 208 209 214 public int read(byte[] b) throws IOException  215 { 216 return this.read(b, 0, b.length); 217 } 218 219 224 public long skip(long n) throws IOException  225 { 226 if (nextvolumenotfound) 227 { 228 createInputStreamToNextVolume(); 230 } 231 long bytesskipped = 0; 232 byte[] buffer = new byte[4096]; 233 try 234 { 235 while (bytesskipped < n) 236 { 237 int maxBytes = (int) Math.min(n - bytesskipped, buffer.length); 238 239 int bytesInBuffer = this.read(buffer, 0, maxBytes); 240 if (bytesInBuffer == -1) 241 throw new IOException ("Unexpected end of stream (installer corrupted?)"); 242 243 bytesskipped += bytesInBuffer; 244 } 245 } 246 catch (VolumeNotFoundException vnfe) 247 { 248 vnfe.setAlreadyskippedbytes(bytesskipped); 249 throw vnfe; 250 } 251 return bytesskipped; 252 } 253 254 259 public String getVolumename() 260 { 261 return volumename; 262 } 263 264 269 public void setVolumename(String volumename) 270 { 271 Debug.trace("new volumename: " + volumename); 272 String volumesuffix = "." + currentvolumeindex; 275 String nextvolumesuffix = "." + (currentvolumeindex + 1); 276 if (volumename.endsWith(volumesuffix)) 277 { 278 this.volumename = volumename.substring(0, volumename.lastIndexOf(volumesuffix)); 279 } 280 else if (volumename.endsWith(nextvolumesuffix)) 281 { 282 this.volumename = volumename.substring(0, volumename.lastIndexOf(nextvolumesuffix)); 283 } 284 else 285 { 286 this.volumename = volumename; 287 } 288 Debug.trace("Set volumename to: " + this.volumename); 289 } 290 291 296 public long getFilepointer() 297 { 298 return filepointer; 299 } 300 301 } 302 | Popular Tags |