001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils;
038:
039: import java.io.BufferedInputStream;
040: import java.io.BufferedOutputStream;
041: import java.io.BufferedReader;
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.FileOutputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.io.OutputStream;
049: import java.io.RandomAccessFile;
050: import java.io.Reader;
051: import java.nio.charset.Charset;
052: import org.netbeans.installer.utils.progress.Progress;
053:
054: /**
055: * @author Danila Dugurov
056: * @author Kirill Sorokin
057: */
058: public class StreamUtils {
059: public static void transferData(final InputStream in,
060: final OutputStream out) throws IOException {
061: final byte[] buffer = new byte[FileUtils.BUFFER_SIZE];
062: int length = 0;
063: while ((length = in.read(buffer)) != -1) {
064: out.write(buffer, 0, length);
065: }
066: out.flush();
067: }
068:
069: public static void transferData(final RandomAccessFile in,
070: final OutputStream out) throws IOException {
071: transferData(in, out, in.length());
072: }
073:
074: public static void transferData(final RandomAccessFile in,
075: final OutputStream out, final Progress progress)
076: throws IOException {
077: transferData(in, out, in.length(), progress);
078: }
079:
080: public static void transferData(final RandomAccessFile in,
081: final OutputStream out, final long max) throws IOException {
082: transferData(in, out, max, new Progress());
083: }
084:
085: public static void transferData(final RandomAccessFile in,
086: final OutputStream out, final long max,
087: final Progress progress) throws IOException {
088: final byte[] buffer = new byte[FileUtils.BUFFER_SIZE];
089:
090: long total = 0;
091: int length = 0;
092:
093: progress.setPercentage(Progress.START);
094: while (((length = in.read(buffer)) != -1) && (total < max)) {
095: total += length;
096: out.write(buffer, 0, (int) (total < max ? length : length
097: - (total - max)));
098:
099: if (total < max) {
100: progress.setPercentage(Progress.COMPLETE * total / max);
101: }
102: }
103: progress.setPercentage(Progress.COMPLETE);
104:
105: out.flush();
106: }
107:
108: public static void transferFile(final File file,
109: final OutputStream out) throws IOException {
110: transferFile(file, out, new Progress());
111: }
112:
113: public static void transferFile(final File file,
114: final OutputStream out, final Progress progress)
115: throws IOException {
116: RandomAccessFile in = null;
117:
118: try {
119: transferData(in = new RandomAccessFile(file, "r"), out,
120: progress);
121: } finally {
122: if (in != null) {
123: try {
124: in.close();
125: } catch (IOException e) {
126: ErrorManager.notifyDebug("Cannot close raf", e);
127: }
128: }
129: }
130: }
131:
132: public static CharSequence readStream(final InputStream input)
133: throws IOException {
134: return readStream(input, Charset
135: .forName(StringUtils.ENCODING_UTF8));
136: }
137:
138: public static CharSequence readStream(final InputStream input,
139: final Charset charset) throws IOException {
140: final Reader reader = new BufferedReader(new InputStreamReader(
141: input, charset));
142: return readReader(reader);
143: }
144:
145: public static CharSequence readReader(final Reader reader)
146: throws IOException {
147: final char[] buffer = new char[FileUtils.BUFFER_SIZE];
148: final StringBuilder stringBuilder = new StringBuilder();
149: int readLength;
150: while ((readLength = reader.read(buffer)) != -1) {
151: stringBuilder.append(buffer, 0, readLength);
152: }
153: return stringBuilder;
154: }
155:
156: public static CharSequence readFile(final File file)
157: throws IOException {
158: return readFile(file, Charset
159: .forName(StringUtils.ENCODING_UTF8));
160: }
161:
162: public static CharSequence readFile(final File file,
163: final Charset charset) throws IOException {
164: final InputStream in = new BufferedInputStream(
165: new FileInputStream(file));
166: try {
167: return readReader(new InputStreamReader(in, charset));
168: } finally {
169: try {
170: in.close();
171: } catch (IOException ignord) {
172: }
173: }
174: }
175:
176: public static void writeChars(final OutputStream out,
177: final CharSequence chars) throws IOException {
178: writeChars(out, chars, Charset
179: .forName(StringUtils.ENCODING_UTF8));
180: }
181:
182: public static void writeChars(final OutputStream out,
183: final CharSequence chars, final Charset charset)
184: throws IOException {
185: out.write(chars.toString().getBytes(charset.name()));
186: }
187:
188: public static void writeChars(final File file,
189: final CharSequence chars) throws IOException {
190: writeChars(file, chars, Charset
191: .forName(StringUtils.ENCODING_UTF8));
192: }
193:
194: public static void writeChars(final File file,
195: final CharSequence chars, final Charset charset)
196: throws IOException {
197: final OutputStream out = new BufferedOutputStream(
198: new FileOutputStream(file));
199:
200: try {
201: writeChars(out, chars, charset);
202: } finally {
203: try {
204: out.close();
205: } catch (IOException ignord) {
206: }
207: }
208: }
209: }
|