1 23 package com.sun.enterprise.management.support; 24 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 29 30 public final class UploadInfo extends UpDownInfo 31 { 32 private final String mName; 33 private final long mTotalSize; 34 private FileOutputStream mOutputStream; 35 private long mWrittenSoFar; 36 37 38 public 39 UploadInfo( 40 final Object id, 41 final String name, 42 final long totalSize ) 43 throws IOException 44 { 45 super( id, createTempFile( id, name, totalSize ) ); 46 47 mName = name; 48 49 mTotalSize = totalSize; 50 51 getFile().createNewFile(); 52 getFile().deleteOnExit(); 53 mOutputStream = new FileOutputStream ( getFile() ); 54 55 mWrittenSoFar = 0; 56 } 57 58 private static File 59 createTempFile( final Object id, final String name, final long totalSize ) 60 throws IOException 61 { 62 final String tempName = (name != null) ? name : id + "_" + totalSize; 63 File actual = new File ( tempName ); 64 if ( actual.exists() ) 65 { 66 actual = File.createTempFile( tempName, null ); 67 } 68 return( actual ); 69 } 70 71 public boolean 72 isDone() 73 { 74 return( mWrittenSoFar == mTotalSize ); 75 } 76 77 78 81 public boolean 82 write( final byte[] bytes ) 83 throws IOException 84 { 85 if ( isDone() || mWrittenSoFar + bytes.length > mTotalSize ) 86 { 87 throw new IllegalArgumentException ( "too many bytes" ); 88 } 89 getOutputStream().write( bytes ); 90 91 mWrittenSoFar += bytes.length; 92 93 if ( isDone() ) 94 { 95 mOutputStream.close(); 96 mOutputStream = null; 97 } 98 99 accessed(); 100 101 return( isDone() ); 102 } 103 104 105 106 public long 107 getTotalSize() 108 { 109 return( mTotalSize ); 110 } 111 112 public void 113 cleanup() 114 throws IOException 115 { 116 if ( mOutputStream != null ) 117 { 118 mOutputStream.close(); 119 } 120 121 getFile().delete(); 122 } 123 124 private FileOutputStream 125 getOutputStream() 126 { 127 return( mOutputStream ); 128 } 129 } 130 131 132 | Popular Tags |