KickJava   Java API By Example, From Geeks To Geeks.

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


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  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/DownloadInfo.java,v 1.4 2005/12/25 03:40:42 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:40:42 $
28  */

29 package com.sun.enterprise.management.support;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35
36 public final class DownloadInfo extends UpDownInfo
37 {
38     private FileInputStream JavaDoc mInputStream;
39     
40     private long mReadSoFar;
41     private final long mTotalSize;
42     private final boolean mDeleteWhenDone;
43     
44         public
45     DownloadInfo(
46         final Object JavaDoc id,
47         final File JavaDoc theFile,
48         final boolean deleteWhenDone )
49         throws IOException JavaDoc
50     {
51         super( id, theFile );
52         
53         mTotalSize = theFile.length();
54         if ( theFile.length() == 0 )
55         {
56             throw new IllegalArgumentException JavaDoc( theFile.toString() );
57         }
58         
59         mReadSoFar = 0;
60         mDeleteWhenDone = deleteWhenDone;
61         
62         if ( mDeleteWhenDone )
63         {
64             theFile.deleteOnExit();
65         }
66         
67         mInputStream = new FileInputStream JavaDoc( 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     /**
93         @return true if done, false otherwise
94      */

95         public synchronized byte[]
96     read( final int requestSize )
97         throws IOException JavaDoc
98     {
99         if ( isDone() )
100         {
101             throw new IllegalArgumentException JavaDoc( "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 JavaDoc();
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 JavaDoc
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