KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > NioUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: NioUtil.java 508 2006-06-02 08:05:25Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.nio.ByteBuffer JavaDoc;
29 import java.nio.channels.Channels JavaDoc;
30 import java.nio.channels.FileChannel JavaDoc;
31 import java.nio.channels.ReadableByteChannel JavaDoc;
32
33 /**
34  * @version $Rev: 508 $ $Date: 2006-06-02 10:05:25 +0200 (ven., 02 juin 2006) $
35  * @since Petals 1.0
36  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
37  */

38 public final class NioUtil {
39
40     /**
41      * Size of buffer in bytes used during some operations.
42      */

43     private static final int BUFFER_SIZE = 4096;
44
45     /**
46      * Do not construct.
47      */

48     private NioUtil() {
49         super();
50     }
51
52     /**
53      * Transfer bytes blocks using direct buffer memory allocation. Relies in
54      * use the faster Java New I/O instead of traditional byte array move.
55      *
56      * @param is
57      * @param aFile
58      * @throws IOException
59      */

60     public static void copyStreamToFile(InputStream JavaDoc is, File JavaDoc aFile)
61         throws IOException JavaDoc {
62
63         ReadableByteChannel JavaDoc inputChannel = Channels.newChannel(is);
64
65         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(aFile);
66         FileChannel JavaDoc outputChannel = fos.getChannel();
67
68         ByteBuffer JavaDoc buffer = ByteBuffer.allocate(BUFFER_SIZE);
69
70         int readBytes;
71         while ((readBytes = inputChannel.read(buffer)) != -1) {
72             if (readBytes > 0) {
73                 buffer.flip();
74                 outputChannel.write(buffer);
75
76                 buffer.clear();
77             }
78         }
79
80         try {
81             inputChannel.close();
82             outputChannel.close();
83             fos.close();
84         } catch (IOException JavaDoc ioe) {
85             // do nothing
86
}
87
88     }
89
90 }
91
Popular Tags