OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / IOToolkit.java
1 /* 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 package gnu.classpath.tools;
22
23 import java.io.BufferedReader;
24 import java.io.File;
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;
34
35 import java.util.Set;
36
37 /**
38  *  Provides various I/O-related helper methods.
39  *
40  *  @author Julian Scheid
41  */
42 public class IOToolkit
43 {
44    /**
45     *  Prevents instantiation.
46     */
47    private IOToolkit() {}
48
49    /**
50     *  Read all binary data from the given InputStream and write it to
51     *  the given OutputStream. This method doesn't close either
52     *  stream.
53     *
54     *  @param in  the stream from which to read data
55     *  @param out the stream to which to write data
56     */
57    public static void copyStream(InputStream in, OutputStream out)
58       throws IOException
59    {
60       byte[] buf = new byte[256];
61       int nread;
62
63       while ((nread = in.read(buf)) >= 0) {
64          out.write(buf, 0, nread);
65       }
66    }
67
68    /**
69     *  Read all character data from the given Reader and write it to
70     *  the given Writer. This method doesn't close either stream.
71     *
72     *  @param in  the Reader from which to read character data
73     *  @param out the Writer to which to write character data
74     */
75    public static void copyStream(Reader in, Writer out)
76       throws IOException 
77    {
78       char[] buf = new char[256];
79       int nread;
80
81       while ((nread = in.read(buf)) >= 0) {
82          out.write(buf, 0, nread);
83       }
84    }
85
86    /**
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.
91     *
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.
99     */
100    public static void copyDirectory(File sourceDir, File targetDir, 
101                                     boolean recursive,
102                                     Set excludeDirs) 
103       throws IOException 
104    {
105       if (!targetDir.exists() && !targetDir.mkdirs()) {
106          throw new IOException("Cannot create directory " + targetDir);
107       }
108
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);
118                }
119                else {
120                   throw new IOException("Cannot create directory " + targetSubDir);
121                }
122             }
123          }
124          else {
125             copyFile(sourceFiles[i], new File(targetDir, sourceFiles[i].getName()));
126          }
127       }
128    }
129
130    /**
131     *  Copy the contents of the input file to the output file. The
132     *  output file's parent directory must exist.
133     *
134     *  @param sourceFile  specifies the file to copy
135     *  @param targetFile  specifies the file to create
136     */
137    public static void copyFile(File sourceFile, File targetFile) 
138       throws IOException 
139    {
140       InputStream in = new FileInputStream(sourceFile);
141       OutputStream out = new FileOutputStream(targetFile);
142       int nread;
143       byte[] buf = new byte[512];
144       while ((nread = in.read(buf)) >= 0) {
145          out.write(buf, 0, nread);
146       }
147       in.close();
148       out.close();
149    }
150
151    /**
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.
154     *
155     *  @param reader the Reader to read characters from
156     *  @return an array with the contents of the Reader
157     */
158    public static char[] readFully(Reader reader)
159       throws IOException
160    {
161       StringWriter writer = new StringWriter();
162       final int readBufferSize = 256;
163       char[] chunk = new char[readBufferSize];
164       int nread;
165       while ((nread=reader.read(chunk))>=0) {
166          writer.write(chunk,0,nread);
167       }
168       StringBuffer buffer = writer.getBuffer();
169       char[] result = new char[buffer.length()];
170       buffer.getChars(0, buffer.length(), result, 0);
171       return result;
172    }
173
174    public static String getLineFromFile(File file, int line)
175       throws IOException
176    {
177       FileReader reader = new FileReader(file);
178       BufferedReader bufferedReader = new BufferedReader(reader);
179       while (line > 1) {
180          bufferedReader.readLine();
181          -- line;
182       }
183       String result = bufferedReader.readLine();
184       reader.close();
185       return result;
186    }
187
188    public static String getColumnDisplayLine(int column)
189    {
190       StringBuffer result = new StringBuffer();
191       while (column > 0) {
192          result.append(' ');
193          --column;
194       }
195       result.append('^');
196       return result.toString();
197    }
198
199 }