| 1 |
779 |
jeremybenn |
/* gnu.classpath.tools.IOToolkit
|
| 2 |
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 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)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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
|
| 19 |
|
|
02111-1307 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
package gnu.classpath.tools;
|
| 39 |
|
|
|
| 40 |
|
|
import java.io.BufferedReader;
|
| 41 |
|
|
import java.io.File;
|
| 42 |
|
|
import java.io.FileInputStream;
|
| 43 |
|
|
import java.io.FileOutputStream;
|
| 44 |
|
|
import java.io.FileReader;
|
| 45 |
|
|
import java.io.IOException;
|
| 46 |
|
|
import java.io.InputStream;
|
| 47 |
|
|
import java.io.OutputStream;
|
| 48 |
|
|
import java.io.Reader;
|
| 49 |
|
|
import java.io.StringWriter;
|
| 50 |
|
|
import java.io.Writer;
|
| 51 |
|
|
|
| 52 |
|
|
import java.util.Set;
|
| 53 |
|
|
|
| 54 |
|
|
/**
|
| 55 |
|
|
* Provides various I/O-related helper methods.
|
| 56 |
|
|
*
|
| 57 |
|
|
* @author Julian Scheid
|
| 58 |
|
|
*/
|
| 59 |
|
|
public class IOToolkit
|
| 60 |
|
|
{
|
| 61 |
|
|
/**
|
| 62 |
|
|
* Prevents instantiation.
|
| 63 |
|
|
*/
|
| 64 |
|
|
private IOToolkit() {}
|
| 65 |
|
|
|
| 66 |
|
|
/**
|
| 67 |
|
|
* Read all binary data from the given InputStream and write it to
|
| 68 |
|
|
* the given OutputStream. This method doesn't close either
|
| 69 |
|
|
* stream.
|
| 70 |
|
|
*
|
| 71 |
|
|
* @param in the stream from which to read data
|
| 72 |
|
|
* @param out the stream to which to write data
|
| 73 |
|
|
*/
|
| 74 |
|
|
public static void copyStream(InputStream in, OutputStream out)
|
| 75 |
|
|
throws IOException
|
| 76 |
|
|
{
|
| 77 |
|
|
byte[] buf = new byte[256];
|
| 78 |
|
|
int nread;
|
| 79 |
|
|
|
| 80 |
|
|
while ((nread = in.read(buf)) >= 0) {
|
| 81 |
|
|
out.write(buf, 0, nread);
|
| 82 |
|
|
}
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
/**
|
| 86 |
|
|
* Read all character data from the given Reader and write it to
|
| 87 |
|
|
* the given Writer. This method doesn't close either stream.
|
| 88 |
|
|
*
|
| 89 |
|
|
* @param in the Reader from which to read character data
|
| 90 |
|
|
* @param out the Writer to which to write character data
|
| 91 |
|
|
*/
|
| 92 |
|
|
public static void copyStream(Reader in, Writer out)
|
| 93 |
|
|
throws IOException
|
| 94 |
|
|
{
|
| 95 |
|
|
char[] buf = new char[256];
|
| 96 |
|
|
int nread;
|
| 97 |
|
|
|
| 98 |
|
|
while ((nread = in.read(buf)) >= 0) {
|
| 99 |
|
|
out.write(buf, 0, nread);
|
| 100 |
|
|
}
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
/**
|
| 104 |
|
|
* Recursively copy the contents of the input directory to the
|
| 105 |
|
|
* output directory. The output directory is created if it doesn't
|
| 106 |
|
|
* exist. If the output directory doesn't exist and can't be
|
| 107 |
|
|
* created, an IOException is thrown.
|
| 108 |
|
|
*
|
| 109 |
|
|
* @param sourceDir source directory from which to copy files
|
| 110 |
|
|
* @param targetDir target directory to which to copy files
|
| 111 |
|
|
* @param recursive if true, recursively copy subdirectoryies
|
| 112 |
|
|
* @param excludeDirs if non null, must be a Set of String. Each
|
| 113 |
|
|
* element from the set specifies the name of a direct
|
| 114 |
|
|
* subdirectory of the source directory which should be excluded
|
| 115 |
|
|
* from recursive copying.
|
| 116 |
|
|
*/
|
| 117 |
|
|
public static void copyDirectory(File sourceDir, File targetDir,
|
| 118 |
|
|
boolean recursive,
|
| 119 |
|
|
Set excludeDirs)
|
| 120 |
|
|
throws IOException
|
| 121 |
|
|
{
|
| 122 |
|
|
if (!targetDir.exists() && !targetDir.mkdirs()) {
|
| 123 |
|
|
throw new IOException("Cannot create directory " + targetDir);
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
File[] sourceFiles = sourceDir.listFiles();
|
| 127 |
|
|
for (int i=0; i<sourceFiles.length; ++i) {
|
| 128 |
|
|
if (sourceFiles[i].isDirectory()) {
|
| 129 |
|
|
if (recursive && (null == excludeDirs
|
| 130 |
|
|
|| !excludeDirs.contains(sourceFiles[i].getName()))) {
|
| 131 |
|
|
File targetSubDir = new File(targetDir,
|
| 132 |
|
|
sourceFiles[i].getName());
|
| 133 |
|
|
if (targetSubDir.exists() || targetSubDir.mkdir()) {
|
| 134 |
|
|
copyDirectory(sourceFiles[i], targetSubDir, recursive, null);
|
| 135 |
|
|
}
|
| 136 |
|
|
else {
|
| 137 |
|
|
throw new IOException("Cannot create directory " + targetSubDir);
|
| 138 |
|
|
}
|
| 139 |
|
|
}
|
| 140 |
|
|
}
|
| 141 |
|
|
else {
|
| 142 |
|
|
copyFile(sourceFiles[i], new File(targetDir, sourceFiles[i].getName()));
|
| 143 |
|
|
}
|
| 144 |
|
|
}
|
| 145 |
|
|
}
|
| 146 |
|
|
|
| 147 |
|
|
/**
|
| 148 |
|
|
* Copy the contents of the input file to the output file. The
|
| 149 |
|
|
* output file's parent directory must exist.
|
| 150 |
|
|
*
|
| 151 |
|
|
* @param sourceFile specifies the file to copy
|
| 152 |
|
|
* @param targetFile specifies the file to create
|
| 153 |
|
|
*/
|
| 154 |
|
|
public static void copyFile(File sourceFile, File targetFile)
|
| 155 |
|
|
throws IOException
|
| 156 |
|
|
{
|
| 157 |
|
|
InputStream in = new FileInputStream(sourceFile);
|
| 158 |
|
|
OutputStream out = new FileOutputStream(targetFile);
|
| 159 |
|
|
int nread;
|
| 160 |
|
|
byte[] buf = new byte[512];
|
| 161 |
|
|
while ((nread = in.read(buf)) >= 0) {
|
| 162 |
|
|
out.write(buf, 0, nread);
|
| 163 |
|
|
}
|
| 164 |
|
|
in.close();
|
| 165 |
|
|
out.close();
|
| 166 |
|
|
}
|
| 167 |
|
|
|
| 168 |
|
|
/**
|
| 169 |
|
|
* Read the (remaining) contents of the given reader into a char
|
| 170 |
|
|
* array. This method doesn't close the reader when it is done.
|
| 171 |
|
|
*
|
| 172 |
|
|
* @param reader the Reader to read characters from
|
| 173 |
|
|
* @return an array with the contents of the Reader
|
| 174 |
|
|
*/
|
| 175 |
|
|
public static char[] readFully(Reader reader)
|
| 176 |
|
|
throws IOException
|
| 177 |
|
|
{
|
| 178 |
|
|
StringWriter writer = new StringWriter();
|
| 179 |
|
|
final int readBufferSize = 256;
|
| 180 |
|
|
char[] chunk = new char[readBufferSize];
|
| 181 |
|
|
int nread;
|
| 182 |
|
|
while ((nread=reader.read(chunk))>=0) {
|
| 183 |
|
|
writer.write(chunk,0,nread);
|
| 184 |
|
|
}
|
| 185 |
|
|
StringBuffer buffer = writer.getBuffer();
|
| 186 |
|
|
char[] result = new char[buffer.length()];
|
| 187 |
|
|
buffer.getChars(0, buffer.length(), result, 0);
|
| 188 |
|
|
return result;
|
| 189 |
|
|
}
|
| 190 |
|
|
|
| 191 |
|
|
public static String getLineFromFile(File file, int line)
|
| 192 |
|
|
throws IOException
|
| 193 |
|
|
{
|
| 194 |
|
|
FileReader reader = new FileReader(file);
|
| 195 |
|
|
BufferedReader bufferedReader = new BufferedReader(reader);
|
| 196 |
|
|
while (line > 1) {
|
| 197 |
|
|
bufferedReader.readLine();
|
| 198 |
|
|
-- line;
|
| 199 |
|
|
}
|
| 200 |
|
|
String result = bufferedReader.readLine();
|
| 201 |
|
|
reader.close();
|
| 202 |
|
|
return result;
|
| 203 |
|
|
}
|
| 204 |
|
|
|
| 205 |
|
|
public static String getColumnDisplayLine(int column)
|
| 206 |
|
|
{
|
| 207 |
|
|
StringBuffer result = new StringBuffer();
|
| 208 |
|
|
while (column > 0) {
|
| 209 |
|
|
result.append(' ');
|
| 210 |
|
|
--column;
|
| 211 |
|
|
}
|
| 212 |
|
|
result.append('^');
|
| 213 |
|
|
return result.toString();
|
| 214 |
|
|
}
|
| 215 |
|
|
|
| 216 |
|
|
}
|