KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > UploadInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.support;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29
30 public final class UploadInfo extends UpDownInfo
31 {
32     private final String JavaDoc mName;
33     private final long mTotalSize;
34     private FileOutputStream JavaDoc mOutputStream;
35     private long mWrittenSoFar;
36     
37     
38         public
39     UploadInfo(
40         final Object JavaDoc id,
41         final String JavaDoc name,
42         final long totalSize )
43         throws IOException JavaDoc
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 JavaDoc( getFile() );
54         
55         mWrittenSoFar = 0;
56     }
57     
58         private static File JavaDoc
59     createTempFile( final Object JavaDoc id, final String JavaDoc name, final long totalSize )
60         throws IOException JavaDoc
61     {
62         final String JavaDoc tempName = (name != null) ? name : id + "_" + totalSize;
63         File JavaDoc actual = new File JavaDoc( 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     /**
79         @return true if done, false otherwise
80      */

81         public boolean
82     write( final byte[] bytes )
83         throws IOException JavaDoc
84     {
85         if ( isDone() || mWrittenSoFar + bytes.length > mTotalSize )
86         {
87             throw new IllegalArgumentException JavaDoc( "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 JavaDoc
115     {
116         if ( mOutputStream != null )
117         {
118             mOutputStream.close();
119         }
120         
121         getFile().delete();
122     }
123     
124         private FileOutputStream JavaDoc
125     getOutputStream()
126     {
127         return( mOutputStream );
128     }
129 }
130
131
132
Popular Tags