1 23 24 29 package com.sun.enterprise.management.support; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 35 36 public final class DownloadInfo extends UpDownInfo 37 { 38 private FileInputStream mInputStream; 39 40 private long mReadSoFar; 41 private final long mTotalSize; 42 private final boolean mDeleteWhenDone; 43 44 public 45 DownloadInfo( 46 final Object id, 47 final File theFile, 48 final boolean deleteWhenDone ) 49 throws IOException 50 { 51 super( id, theFile ); 52 53 mTotalSize = theFile.length(); 54 if ( theFile.length() == 0 ) 55 { 56 throw new IllegalArgumentException ( theFile.toString() ); 57 } 58 59 mReadSoFar = 0; 60 mDeleteWhenDone = deleteWhenDone; 61 62 if ( mDeleteWhenDone ) 63 { 64 theFile.deleteOnExit(); 65 } 66 67 mInputStream = new FileInputStream ( theFile ); 68 69 assert( ! isDone() ); 70 } 71 72 73 public final long 74 getLength() 75 { 76 return( mTotalSize ); 77 } 78 79 private final long 80 getRemaining() 81 { 82 return( mTotalSize - mReadSoFar ); 83 } 84 85 public boolean 86 isDone() 87 { 88 return( mReadSoFar == mTotalSize ); 89 } 90 91 92 95 public synchronized byte[] 96 read( final int requestSize ) 97 throws IOException 98 { 99 if ( isDone() ) 100 { 101 throw new IllegalArgumentException ( "operation has been completed" ); 102 } 103 104 final long remaining = getRemaining(); 105 106 byte[] bytes = null; 107 if ( remaining != 0 ) 108 { 109 final long actual = remaining < requestSize ? remaining : requestSize; 110 111 bytes = new byte[ (int)actual ]; 112 final int numRead = mInputStream.read( bytes ); 113 if ( numRead != bytes.length ) 114 { 115 throw new IOException (); 116 } 117 118 mReadSoFar += numRead; 119 120 if ( isDone() ) 121 { 122 cleanup(); 123 } 124 125 accessed(); 126 } 127 128 return( bytes ); 129 } 130 131 public synchronized void 132 cleanup() 133 throws IOException 134 { 135 if ( mInputStream != null ) 136 { 137 mInputStream.close(); 138 mInputStream = null; 139 } 140 141 if ( mDeleteWhenDone ) 142 { 143 getFile().delete(); 144 } 145 } 146 } 147 148 | Popular Tags |