KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.admin.common;
25
26 //JDK imports
27
import java.io.Serializable JavaDoc;
28
29 /**
30     A Class that represents a Chunk of Bytes. This is designed to be used
31     to transfer bytes of data between remote entities. There are some properties
32     of the class that will allow users of this class to take specific
33     actions. Maximum size of the chunk is 64 KBytes. Needs to be Serializable,
34     for ByteChunks may transmit over wire.
35     <p>
36     Chunks are generally parts of file identified by a name. Note that this
37     class is modeled to be immutable.
38     <p>
39     @author Kedar Mhaswade
40     @version 1.0
41 */

42
43 public class ByteChunk implements Serializable JavaDoc
44 {
45     /* javac 1.3 generated serialVersionUID */
46     public static final long serialVersionUID = 9100504074948693275L;
47     public static final int kChunkMaxSize = 10485760;
48     public static final int kChunkMinSize = 0; // 0 Bytes
49

50     private int mSize;
51     private boolean mIsLast;
52     private boolean mIsFirst;
53     private byte[] mBytes;
54     private String JavaDoc mChunkedFileName;
55     private String JavaDoc mTargetDir;
56     private String JavaDoc mUniqueId;
57     private long mTotalFileSize;
58
59     /**
60         Creates new ByteChunk with the specified size. The chunk may not be
61         larger than the size specfied by kChunkMaxSize.
62         
63         @throws IllegalArgumentException if the size of Chunk is more than
64         kChunkSize or less than kChunkMinSize..
65      
66         @param byteArray specifies the array of bytes that constitutes chunk.
67         @param forFileName specifies the name of the file whose part is it.
68         @param isFirst boolean specifying if this is the first of the chunks.
69         @param isLast boolean specifying if this is the first of the chunks.
70         @param uniqueId String specifies an unique id for this series of chunks.
71                         This has to be same in all chunks of the series.
72         @param totalFileSize long specifies total file size of the chunked file.
73     */

74     
75     public ByteChunk(byte[] byteArray, String JavaDoc forFileName,
76         boolean isFirst, boolean isLast, String JavaDoc uniqueId, long totalFileSize)
77     {
78         int size =0;
79         if (byteArray != null) {
80             size = byteArray.length;
81             if ( size < kChunkMinSize || size > kChunkMaxSize)
82             {
83                 throw new IllegalArgumentException JavaDoc(size + "");
84             }
85         }
86         mSize = size;
87         mIsFirst = isFirst;
88         mIsLast = isLast;
89         mBytes = byteArray;
90         mChunkedFileName = forFileName;
91         mUniqueId = uniqueId;
92         mTotalFileSize = totalFileSize;
93     }
94  
95     /**
96         Creates new ByteChunk with the specified size. The chunk may not be
97         larger than the size specfied by kChunkMaxSize.
98         
99         @throws IllegalArgumentException if the size of Chunk is more than
100         kChunkSize or less than kChunkMinSize..
101      
102         @param byteArray specifies the array of bytes that constitutes chunk.
103         @param forFileName specifies the name of the file whose part is it.
104         @param isFirst boolean specifying if this is the first of the chunks.
105         @param isLast boolean specifying if this is the first of the chunks.
106     */

107     
108     public ByteChunk(byte[] byteArray, String JavaDoc forFileName,
109         boolean isFirst, boolean isLast)
110     {
111         this(byteArray,forFileName,isFirst,isLast,forFileName, -1);
112     }
113  
114     /**
115         Determines whether this is the last Chunk in a series of Chunk transfer.
116         
117         @return true if this is last Chunk, false otherwize.
118     */

119     
120     public boolean isLast()
121     {
122         return ( mIsLast );
123     }
124
125     /**
126         Determines whether this is the last Chunk in a series of Chunk transfer.
127         
128         @return true if this is last Chunk, false otherwize.
129     */

130
131     public boolean isFirst()
132     {
133         return ( mIsFirst );
134     }
135     
136     /**
137         Gets the actual size of the chunk. The chunk will have exactly this many
138         bytes in the underlying byte array.
139      
140         @return integer indicating the size of the chunk.
141     */

142     public int getSize()
143     {
144         return ( mSize );
145     }
146     /**
147         Gets the array of actual bytes.
148      
149         @return byte[] with actual bytes.
150     */

151     public byte[] getBytes()
152     {
153         return mBytes;
154     }
155     
156     /**
157         Returns the <code> name </code> of the file that was chunked.
158      
159         @return String representing the name of file (whose this is a chunk).
160     */

161     public String JavaDoc getChunkedFileName()
162     {
163         return ( mChunkedFileName );
164     }
165
166     /**
167      * Returns the name of the target directory to which this chunk
168      * is copied.
169      */

170     public String JavaDoc getTargetDir()
171     {
172         return ( mTargetDir );
173     }
174
175     /**
176      * Sets the target dir to which this chunk must be copied.
177      */

178     public void setTargetDir(String JavaDoc targetDir)
179     {
180         mTargetDir = targetDir;
181     }
182     
183     /**
184      * Returns the unique id for this series of chunks.
185      */

186     public String JavaDoc getId() {
187         return mUniqueId;
188     }
189
190     /**
191      * returns the total file size.
192      */

193     public long getTotalFileSize() {
194         return mTotalFileSize;
195     }
196 }
197
Popular Tags