]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/StreamUtils.java
Fix issue with CMS deployment marked twice as available when state has
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / StreamUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.argeo.util;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.io.Reader;
22 import java.io.Writer;
23
24 /** Utilities to be used when APache COmmons IO is not available. */
25 class StreamUtils {
26 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
27
28 /*
29 * APACHE COMMONS IO (inspired)
30 */
31
32 /** @return the number of bytes */
33 public static Long copy(InputStream in, OutputStream out)
34 throws IOException {
35 Long count = 0l;
36 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
37 while (true) {
38 int length = in.read(buf);
39 if (length < 0)
40 break;
41 out.write(buf, 0, length);
42 count = count + length;
43 }
44 return count;
45 }
46
47 /** @return the number of chars */
48 public static Long copy(Reader in, Writer out) throws IOException {
49 Long count = 0l;
50 char[] buf = new char[DEFAULT_BUFFER_SIZE];
51 while (true) {
52 int length = in.read(buf);
53 if (length < 0)
54 break;
55 out.write(buf, 0, length);
56 count = count + length;
57 }
58 return count;
59 }
60
61 public static void closeQuietly(InputStream in) {
62 if (in != null)
63 try {
64 in.close();
65 } catch (Exception e) {
66 //
67 }
68 }
69
70 public static void closeQuietly(OutputStream out) {
71 if (out != null)
72 try {
73 out.close();
74 } catch (Exception e) {
75 //
76 }
77 }
78
79 public static void closeQuietly(Reader in) {
80 if (in != null)
81 try {
82 in.close();
83 } catch (Exception e) {
84 //
85 }
86 }
87
88 public static void closeQuietly(Writer out) {
89 if (out != null)
90 try {
91 out.close();
92 } catch (Exception e) {
93 //
94 }
95 }
96 }