You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2007/02/11 06:39:03 UTC
svn commit: r505843 - in /incubator/uima/uimaj/trunk/uimaj-core/src:
main/java/org/apache/uima/internal/util/FileUtils.java
test/java/org/apache/uima/cas/test/SerializationNoMDTest.java
Author: schor
Date: Sat Feb 10 21:39:01 2007
New Revision: 505843
URL: http://svn.apache.org/viewvc?view=rev&rev=505843
Log:
UIMA-210 fixes. Removed unused and incorrect method byteStream2String
Modified:
incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/FileUtils.java
incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationNoMDTest.java
Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/FileUtils.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/FileUtils.java?view=diff&rev=505843&r1=505842&r2=505843
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/FileUtils.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/FileUtils.java Sat Feb 10 21:39:01 2007
@@ -21,7 +21,6 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
-import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
@@ -111,39 +110,30 @@
/**
* Read a bufferedReader into a string, using the default platform encoding.
*
- * @param reader to be read in, not buffered (this routine buffers it).
- * @param length of the input
+ * @param reader to be read in
+ * @param bufSize - size of stream, in bytes. Size in chars is <= size in bytes, because
+ * chars take 1 or more bytes to encode.
* @return String The contents of the stream.
* @throws IOException
* Various I/O errors.
*/
- public static String reader2String(Reader reader, int length) throws IOException {
- BufferedReader bReader = new BufferedReader(reader);
- // Read the file into a string using a char buffer.
- char[] buf = new char[length];
+ public static String reader2String(Reader reader, int bufSize) throws IOException {
+ char[] buf = new char[bufSize];
+ int read_so_far = 0;
try {
- // will read all the chars of the file, calling read repeatedly
- // as needed in the underlying layer
- bReader.read(buf, 0, length);
+ while (read_so_far < bufSize) {
+ int count = reader.read(buf, read_so_far, bufSize - read_so_far);
+ if (0 > count) {
+ break;
+ }
+ read_so_far += count;
+ }
} finally {
- bReader.close();
+ reader.close();
}
- return new String(buf);
+ return new String(buf, 0, read_so_far);
}
- public static String byteStream2String(Reader reader, int length) throws IOException {
- BufferedReader bReader = new BufferedReader(reader);
- // Read the file into a string using a char buffer.
- char[] buf = new char[length];
- try {
- // will read all the chars of the file, calling read repeatedly
- // as needed in the underlying layer
- bReader.read(buf, 0, length);
- } finally {
- bReader.close();
- }
- return new String(buf);
- }
/**
* Read the contents of a file into a string, using the default platform encoding.
@@ -173,6 +163,8 @@
* Various I/O errors.
*/
public static String file2String(File file, String fileEncoding) throws IOException {
+ if (null == fileEncoding) // use default
+ return file2String(file);
return reader2String(
new InputStreamReader(new FileInputStream(file), fileEncoding),
(int) file.length());
@@ -338,20 +330,24 @@
throw new IOException("Can't write output file: " + outFile);
}
byte[] bytes = new byte[(int) file.length()];
- BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
- // must use buffered stream to have the read(...) read the whole file
- is.read(bytes);
- is.close();
- BufferedOutputStream os = null;
+ FileInputStream is = null;
+ FileOutputStream os = null;
try {
- os = new BufferedOutputStream(new FileOutputStream(outFile));
- os.write(bytes);
- os.close();
+ is = new FileInputStream(file);
+ os = new FileOutputStream(outFile);
+
+ while (true) {
+ int count = is.read(bytes);
+ if (0 > count)
+ break;
+ os.write(bytes, 0, count);
+ }
} finally {
- if (os != null) {
+ if (null != is)
+ is.close();
+ if (null != os)
os.close();
- }
- }
+ }
}
public static void main(String[] args) {
Modified: incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationNoMDTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationNoMDTest.java?view=diff&rev=505843&r1=505842&r2=505843
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationNoMDTest.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SerializationNoMDTest.java Sat Feb 10 21:39:01 2007
@@ -374,19 +374,25 @@
*/
public static String file2String(File file) throws IOException {
// Read the file into a string using a char buffer.
- BufferedReader reader = null;
- int fileLength = (int) file.length();
- char[] buf = new char[fileLength];
+ FileReader reader = null;
+ int bufSize = (int) file.length(); // length in bytes >= length in chars due to encoding
+ char[] buf = new char[bufSize];
+ int read_so_far = 0;
try {
- reader = new BufferedReader(new FileReader(file));
- // will read all the chars of the file, calling read repeatedly
- // as needed in the underlying layer
- reader.read(buf, 0, fileLength);
+ reader = new FileReader(file);
+ while (read_so_far < bufSize) {
+ int count = reader.read(buf, read_so_far, bufSize - read_so_far);
+ if (count < 0) {
+ break;
+ }
+ read_so_far += count;
+ }
+
} finally {
if (null != reader)
reader.close();
}
- return new String(buf);
+ return new String(buf, 0, read_so_far);
}
/**