1 /* gnu.classpath.tools.IOToolkit
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 package gnu.classpath.tools;
23 import java.io.BufferedReader;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.io.Reader;
32 import java.io.StringWriter;
33 import java.io.Writer;
38 * Provides various I/O-related helper methods.
40 * @author Julian Scheid
42 public class IOToolkit
45 * Prevents instantiation.
47 private IOToolkit() {}
50 * Read all binary data from the given InputStream and write it to
51 * the given OutputStream. This method doesn't close either
54 * @param in the stream from which to read data
55 * @param out the stream to which to write data
57 public static void copyStream(InputStream in, OutputStream out)
60 byte[] buf = new byte[256];
63 while ((nread = in.read(buf)) >= 0) {
64 out.write(buf, 0, nread);
69 * Read all character data from the given Reader and write it to
70 * the given Writer. This method doesn't close either stream.
72 * @param in the Reader from which to read character data
73 * @param out the Writer to which to write character data
75 public static void copyStream(Reader in, Writer out)
78 char[] buf = new char[256];
81 while ((nread = in.read(buf)) >= 0) {
82 out.write(buf, 0, nread);
87 * Recursively copy the contents of the input directory to the
88 * output directory. The output directory is created if it doesn't
89 * exist. If the output directory doesn't exist and can't be
90 * created, an IOException is thrown.
92 * @param sourceDir source directory from which to copy files
93 * @param targetDir target directory to which to copy files
94 * @param recursive if true, recursively copy subdirectoryies
95 * @param excludeDirs if non null, must be a Set of String. Each
96 * element from the set specifies the name of a direct
97 * subdirectory of the source directory which should be excluded
98 * from recursive copying.
100 public static void copyDirectory(File sourceDir, File targetDir,
105 if (!targetDir.exists() && !targetDir.mkdirs()) {
106 throw new IOException("Cannot create directory " + targetDir);
109 File[] sourceFiles = sourceDir.listFiles();
110 for (int i=0; i<sourceFiles.length; ++i) {
111 if (sourceFiles[i].isDirectory()) {
112 if (recursive && (null == excludeDirs
113 || !excludeDirs.contains(sourceFiles[i].getName()))) {
114 File targetSubDir = new File(targetDir,
115 sourceFiles[i].getName());
116 if (targetSubDir.exists() || targetSubDir.mkdir()) {
117 copyDirectory(sourceFiles[i], targetSubDir, recursive, null);
120 throw new IOException("Cannot create directory " + targetSubDir);
125 copyFile(sourceFiles[i], new File(targetDir, sourceFiles[i].getName()));
131 * Copy the contents of the input file to the output file. The
132 * output file's parent directory must exist.
134 * @param sourceFile specifies the file to copy
135 * @param targetFile specifies the file to create
137 public static void copyFile(File sourceFile, File targetFile)
140 InputStream in = new FileInputStream(sourceFile);
141 OutputStream out = new FileOutputStream(targetFile);
143 byte[] buf = new byte[512];
144 while ((nread = in.read(buf)) >= 0) {
145 out.write(buf, 0, nread);
152 * Read the (remaining) contents of the given reader into a char
153 * array. This method doesn't close the reader when it is done.
155 * @param reader the Reader to read characters from
156 * @return an array with the contents of the Reader
158 public static char[] readFully(Reader reader)
161 StringWriter writer = new StringWriter();
162 final int readBufferSize = 256;
163 char[] chunk = new char[readBufferSize];
165 while ((nread=reader.read(chunk))>=0) {
166 writer.write(chunk,0,nread);
168 StringBuffer buffer = writer.getBuffer();
169 char[] result = new char[buffer.length()];
170 buffer.getChars(0, buffer.length(), result, 0);
174 public static String getLineFromFile(File file, int line)
177 FileReader reader = new FileReader(file);
178 BufferedReader bufferedReader = new BufferedReader(reader);
180 bufferedReader.readLine();
183 String result = bufferedReader.readLine();
188 public static String getColumnDisplayLine(int column)
190 StringBuffer result = new StringBuffer();
196 return result.toString();