Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UploadFilePacket |
|
| 2.75;2,75 |
1 | // | |
2 | // $Id$ | |
3 | // | |
4 | // jupload - A file upload applet. | |
5 | // | |
6 | // Copyright 2010 The JUpload Team | |
7 | // | |
8 | // Created: 27 janv. 2010 | |
9 | // Creator: etienne_sf | |
10 | // Last modified: $Date$ | |
11 | // | |
12 | // This program is free software; you can redistribute it and/or modify | |
13 | // it under the terms of the GNU General Public License as published by | |
14 | // the Free Software Foundation; either version 2 of the License, or | |
15 | // (at your option) any later version. | |
16 | // | |
17 | // This program is distributed in the hope that it will be useful, | |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | // GNU General Public License for more details. | |
21 | // | |
22 | // You should have received a copy of the GNU General Public License | |
23 | // along with this program; if not, write to the Free Software | |
24 | // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | ||
26 | package wjhk.jupload2.upload; | |
27 | ||
28 | import java.util.ArrayList; | |
29 | ||
30 | import wjhk.jupload2.policies.UploadPolicy; | |
31 | ||
32 | /** | |
33 | * This file contains a packet of files, which will be sent in one request to | |
34 | * the server. | |
35 | * | |
36 | * @author etienne_sf | |
37 | */ | |
38 | 0 | @SuppressWarnings("serial") |
39 | public class UploadFilePacket extends ArrayList<UploadFileData> { | |
40 | ||
41 | /** | |
42 | * The uploadPolicy, useful to get the max number of files and max packet | |
43 | * size ... and perhaps other parameters in the future. | |
44 | */ | |
45 | 39 | UploadPolicy uploadPolicy = null; |
46 | ||
47 | /** | |
48 | * The sum of the size of all files in the packet. | |
49 | */ | |
50 | 39 | long nbBytes = 0; |
51 | ||
52 | /** The maximum number of bytes in one packet to the server */ | |
53 | 39 | long maxNbBytes = -1; |
54 | ||
55 | /** The maximum number of files in one packet to the server */ | |
56 | 39 | long maxNbFiles = -1; |
57 | ||
58 | /** | |
59 | * The standard constructor. | |
60 | * | |
61 | * @param uploadPolicy | |
62 | */ | |
63 | UploadFilePacket(UploadPolicy uploadPolicy) { | |
64 | // nbFilesPerRequest may be very very big. Let's have a more realistic | |
65 | // value, in this case (to avoid outOfMemoryError) | |
66 | 78 | super(Math.min(uploadPolicy.getNbFilesPerRequest(), uploadPolicy |
67 | 39 | .getContext().getUploadPanel().getFilePanel().getFilesLength())); |
68 | 39 | this.uploadPolicy = uploadPolicy; |
69 | 39 | this.maxNbBytes = this.uploadPolicy.getMaxChunkSize(); |
70 | 78 | this.maxNbFiles = Math.min(uploadPolicy.getNbFilesPerRequest(), |
71 | 39 | uploadPolicy.getContext().getUploadPanel().getFilePanel() |
72 | 39 | .getFilesLength()); |
73 | 39 | } |
74 | ||
75 | /** | |
76 | * Checks if this packet can accept this file, according to the current | |
77 | * {@link UploadPolicy}. | |
78 | * | |
79 | * @param uploadFileData | |
80 | * @return True if the file can be added, false otherwise. | |
81 | */ | |
82 | public synchronized boolean canAdd(UploadFileData uploadFileData) { | |
83 | // Here is the list of conditions. This code could be smalled. But I | |
84 | // want it to be clear. | |
85 | 120 | if (size() == 0) { |
86 | 14 | return true; |
87 | 106 | } else if (size() == this.maxNbFiles) { |
88 | // The packet is already full of files | |
89 | 2 | return false; |
90 | 104 | } else if (this.nbBytes + uploadFileData.getUploadLength() > maxNbBytes) { |
91 | // The packet would be too big | |
92 | 0 | return false; |
93 | } else { | |
94 | // No reason to refuse this packet ... | |
95 | 104 | return true; |
96 | } | |
97 | } | |
98 | ||
99 | /** | |
100 | * Indicates whether it is possible to add a file or not, to this packet, | |
101 | * according to the current upload policy. | |
102 | * | |
103 | * @return true if the packet is full, that is: no file can be added to this | |
104 | * packet. | |
105 | */ | |
106 | public synchronized boolean isFull() { | |
107 | 112 | return (size() == this.maxNbFiles) || (this.nbBytes >= maxNbBytes); |
108 | } | |
109 | ||
110 | /** | |
111 | * @param uploadFileData The file to add to the packet | |
112 | * @return true if the collection changed, that is: if the file was actually | |
113 | * added. false if the Collection didn't change, that is: the packet | |
114 | * is full, or th file is already there. | |
115 | */ | |
116 | public synchronized boolean add(UploadFileData uploadFileData) { | |
117 | 117 | if (!canAdd(uploadFileData)) { |
118 | 1 | return false; |
119 | 116 | } else if (!super.add(uploadFileData)) { |
120 | 0 | return false; |
121 | } else { | |
122 | // The file was correctly added. | |
123 | 116 | nbBytes += uploadFileData.getUploadLength(); |
124 | ; | |
125 | 116 | return true; |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * @return the poisonned status. Returns always false, as this instance is a | |
131 | * true one. false indicates the 'End Of Queue' marker in the | |
132 | * preparedFileQueue, which is not the case here | |
133 | * @see UploadFileDataPoisonned | |
134 | */ | |
135 | public boolean isPoisonned() { | |
136 | 2 | return false; |
137 | } | |
138 | ||
139 | // //////////////////////////////////////////////////////////////////////////// | |
140 | // Some ArrayList methods are prohibited here | |
141 | // //////////////////////////////////////////////////////////////////////////// | |
142 | /** | |
143 | * Prohibited ! | |
144 | * | |
145 | * @see ArrayList#remove(int) | |
146 | */ | |
147 | public UploadFileData remove(int index) { | |
148 | 0 | throw new java.lang.UnsupportedOperationException( |
149 | "Removing a file from an UploadFilePacket is prohibited"); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Prohibited ! | |
154 | * | |
155 | * @see ArrayList#remove(Object) | |
156 | */ | |
157 | public boolean remove(Object o) { | |
158 | 0 | throw new java.lang.UnsupportedOperationException( |
159 | "Removing a file from an UploadFilePacket is prohibited"); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Prohibited ! | |
164 | * | |
165 | * @see ArrayList#removeRange(int, int) | |
166 | */ | |
167 | protected void removeRange(int fromIndex, int toIndex) { | |
168 | 0 | throw new java.lang.UnsupportedOperationException( |
169 | "Removing a file from an UploadFilePacket is prohibited"); | |
170 | } | |
171 | } |