KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > PartHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 public class PartHelper {
22     private static final int BUFFER_SIZE = 32768;
23
24     /**
25      * Reads an inputstream till the end into a byte array. Closes the input stream
26      * whatever happens. Optimized for the case where we know the size of the input
27      * on beforehand.
28      *
29      * @param is the input stream to be read
30      * @param sizeHint optional, specify -1 if nothing useful
31      * @throws IOException
32      */

33     public static byte[] streamToByteArrayAndClose(InputStream JavaDoc is, int sizeHint) throws IOException JavaDoc {
34         if (is == null)
35             throw new IllegalArgumentException JavaDoc("\"is\" param may not be null");
36
37         try {
38             byte[] buffer = new byte[sizeHint != -1 ? sizeHint : BUFFER_SIZE];
39
40             int read = is.read(buffer);
41             if (read == -1) {
42                 return new byte[0];
43             } else {
44                 int c = is.read();
45                 if (c == -1) {
46                     // we read the entire stream in one go
47
return read == buffer.length ? buffer : cutBuffer(buffer, read);
48                 } else {
49                     // grow buffer if necessary to store c
50
if (read == buffer.length)
51                         buffer = growBuffer(buffer);
52                     buffer[read] = (byte)c;
53                     int pos = read + 1;
54
55                     while (true) {
56                         read = is.read(buffer, pos, buffer.length - pos);
57                         if (read != -1) {
58                             if (pos + read == buffer.length) {
59                                 // before growing, check we're not at the end
60
c = is.read();
61                                 if (c == -1)
62                                     return buffer;
63                                 buffer = growBuffer(buffer);
64                                 buffer[pos + read] = (byte)c;
65                                 pos = pos + read + 1;
66                             } else {
67                                 pos = pos + read;
68                             }
69                         } else {
70                             return cutBuffer(buffer, pos);
71                         }
72                     }
73                 }
74             }
75         } finally {
76             is.close();
77         }
78     }
79
80     private static byte[] growBuffer(byte[] buffer) {
81         byte[] newBuffer = new byte[buffer.length + BUFFER_SIZE];
82         System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
83         return newBuffer;
84     }
85
86     private static byte[] cutBuffer(byte[] buffer, int length) {
87         byte[] newBuffer = new byte[length];
88         System.arraycopy(buffer, 0, newBuffer, 0, length);
89         return newBuffer;
90     }
91 }
92
Popular Tags