KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > Misc


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.appserv.management.helper;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import com.sun.appserv.management.base.UploadDownloadMgr;
30
31 /**
32     Miscellaneous helper routines specific to AMX. These routines
33     may be moved prior to the release of 9.0
34     @since AppServer 9.0
35  */

36 public class Misc
37 {
38     private Misc() {}
39     
40     /**
41         Upload a file to the server and return an opaque identifier which server-side
42         code may use to identify and use the file. Uploading a file is typically done
43         prior to deploying it, but may be done for other purposes.
44         
45         @param mgr an instance of UploadDownloadMgr,
46             obtained via getDomainRoot().getUploadDownloadMgr()
47         @param theFile the file to upload
48         @return an upload ID
49      */

50         public static Object JavaDoc
51     uploadFile(
52         final UploadDownloadMgr mgr,
53         final File JavaDoc theFile )
54         throws IOException JavaDoc
55     {
56         final FileInputStream JavaDoc input = new FileInputStream JavaDoc( theFile );
57         final long length = input.available();
58         final Object JavaDoc uploadID = mgr.initiateUpload( theFile.getName(), length );
59             
60         try
61         {
62             final int chunkSize = 256 * 1024;
63             long remaining = length;
64             while ( remaining != 0 )
65             {
66                 final int actual = remaining < chunkSize ? (int)remaining : chunkSize;
67                 
68                 final byte[] bytes = new byte[ actual ];
69                 final int num = input.read( bytes );
70                 if ( num != actual )
71                 {
72                     throw new IOException JavaDoc();
73                 }
74
75                 mgr.uploadBytes( uploadID, bytes );
76                 remaining -= actual;
77             }
78         }
79         finally
80         {
81             input.close();
82         }
83         
84         return( uploadID );
85     }
86 }
87
88
89
Popular Tags