KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > DownloadRequestInfo


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.admin.common;
24
25 import java.io.File JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * Contains information pertaining to a download request.
30  *
31  * @author Nazrul Islam
32  * @since J2SE1.4
33  */

34 public class DownloadRequestInfo implements Serializable JavaDoc {
35
36     /**
37      * Constructor.
38      */

39     private DownloadRequestInfo() {
40         _downloadFilePath = null;
41         _numChunks = 0;
42         _numBytesSent = 0;
43         _isPrepared = false;
44         _chunkIndex = -1;
45         _chunk = null;
46     }
47
48     public DownloadRequestInfo(File JavaDoc f) {
49         super();
50
51         if (f.exists()) {
52             _downloadFilePath = f.getAbsolutePath();
53
54             // total size of the download file
55
_totalFileSize = f.length();
56
57             // total number of chunks
58
_numChunks = Math.round(_totalFileSize/ByteChunk.kChunkMaxSize);
59
60             // verifies the total
61
if (_numChunks * ByteChunk.kChunkMaxSize < _totalFileSize) {
62                 _numChunks += 1;
63             }
64
65             // sets the prepared flag
66
_isPrepared = true;
67             _chunkIndex = 0;
68         }
69     }
70
71     /**
72      * Returns the download file path.
73      *
74      * @return download file path
75      */

76     public String JavaDoc getDownloadFilePath() {
77         return _downloadFilePath;
78     }
79
80     /**
81      * Sets the download file path.
82      *
83      * @param f download file path
84      */

85     void setDownloadFilePath(String JavaDoc f) {
86         _downloadFilePath = f;
87     }
88
89     /**
90      * Returns total number of download chunks for this request.
91      *
92      * @return total number of download chunks
93      */

94     public int getNumberOfChunks() {
95         return _numChunks;
96     }
97
98     /**
99      * Sets the total number of download chunks.
100      *
101      * @param n total number of download chunks
102      */

103     void setNumberOfChunks(int n) {
104         _numChunks = n;
105     }
106
107     /**
108      * Returns true if the request info is computed
109      *
110      * @return if the request is prepared
111      */

112     public boolean isPrepared() {
113         return _isPrepared;
114     }
115
116     /**
117      * Sets the prepared flag for this request.
118      *
119      * @param p prepared flag for this request.
120      */

121     void setPrepared(boolean p) {
122         _isPrepared = p;
123     }
124
125     /**
126      * Returns the current chunk index.
127      *
128      * @return the current chunk index
129      */

130     public int getChunkIndex() {
131         return _chunkIndex;
132     }
133
134     /**
135      * Sets the current chunk index.
136      *
137      * @param i current chunk index
138      */

139     void setChunkIndex(int i) {
140         _chunkIndex = i;
141
142         // resets the byte chunk
143
_chunk = null;
144     }
145
146     /**
147      * Returns the total number of bytes downloaded.
148      *
149      * @return total number of bytes downloaded
150      */

151     public long getNumberOfBytesSent() {
152         return _numBytesSent;
153     }
154
155     /**
156      * Increments the total number of bytes sent out.
157      *
158      * @param bytesRead number of bytes to be added to the total
159      */

160     public void incrementNumberOfBytesSent(int bytesRead) {
161         _numBytesSent += bytesRead;
162     }
163
164     /**
165      * Returns true if this is the first chunk to download.
166      *
167      * @return true if this is the first chunk
168      */

169     public boolean isFirstChunk() {
170         return (_chunkIndex == 0);
171     }
172
173     /**
174      * Returns true if this the last chunk to download.
175      *
176      * @return true if this the last chunk.
177      */

178     public boolean isLastChunk() {
179         return (_chunkIndex == (_numChunks-1));
180     }
181
182     /**
183      * Returns the byte chunk for this request.
184      *
185      * @return byte chunk
186      */

187     public ByteChunk getChunk() {
188         return _chunk;
189     }
190
191     /**
192      * Sets the byte chunk for this request.
193      *
194      * @param chunk byte chunk
195      */

196     public void setChunk(ByteChunk chunk) {
197         _chunk = chunk;
198     }
199
200     /**
201      * Returns the total file size for this request.
202      *
203      * @return total file size
204      */

205     public long getTotalFileSize() {
206         return _totalFileSize;
207     }
208
209     // ---- VARIABLES - PRIVATE -------------
210
private String JavaDoc _downloadFilePath;
211     private int _numChunks;
212     private long _numBytesSent;
213     private boolean _isPrepared;
214     private int _chunkIndex;
215     private ByteChunk _chunk;
216     private long _totalFileSize;
217 }
218
Popular Tags