* commit 'd3036f7e89747d7347eb8b94c5517162cc71b5af': use BufferedInputStream in readTextFile
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package android.os;
|
package android.os;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -143,12 +144,16 @@ public class FileUtils {
|
|||||||
*/
|
*/
|
||||||
public static String readTextFile(File file, int max, String ellipsis) throws IOException {
|
public static String readTextFile(File file, int max, String ellipsis) throws IOException {
|
||||||
InputStream input = new FileInputStream(file);
|
InputStream input = new FileInputStream(file);
|
||||||
|
// wrapping a BufferedInputStream around it because when reading /proc with unbuffered
|
||||||
|
// input stream, bytes read not equal to buffer size is not necessarily the correct
|
||||||
|
// indication for EOF; but it is true for BufferedInputStream due to its implementation.
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(input);
|
||||||
try {
|
try {
|
||||||
long size = file.length();
|
long size = file.length();
|
||||||
if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the first N bytes
|
if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the first N bytes
|
||||||
if (size > 0 && (max == 0 || size < max)) max = (int) size;
|
if (size > 0 && (max == 0 || size < max)) max = (int) size;
|
||||||
byte[] data = new byte[max + 1];
|
byte[] data = new byte[max + 1];
|
||||||
int length = input.read(data);
|
int length = bis.read(data);
|
||||||
if (length <= 0) return "";
|
if (length <= 0) return "";
|
||||||
if (length <= max) return new String(data, 0, length);
|
if (length <= max) return new String(data, 0, length);
|
||||||
if (ellipsis == null) return new String(data, 0, max);
|
if (ellipsis == null) return new String(data, 0, max);
|
||||||
@@ -161,7 +166,7 @@ public class FileUtils {
|
|||||||
if (last != null) rolled = true;
|
if (last != null) rolled = true;
|
||||||
byte[] tmp = last; last = data; data = tmp;
|
byte[] tmp = last; last = data; data = tmp;
|
||||||
if (data == null) data = new byte[-max];
|
if (data == null) data = new byte[-max];
|
||||||
len = input.read(data);
|
len = bis.read(data);
|
||||||
} while (len == data.length);
|
} while (len == data.length);
|
||||||
|
|
||||||
if (last == null && len <= 0) return "";
|
if (last == null && len <= 0) return "";
|
||||||
@@ -178,12 +183,13 @@ public class FileUtils {
|
|||||||
int len;
|
int len;
|
||||||
byte[] data = new byte[1024];
|
byte[] data = new byte[1024];
|
||||||
do {
|
do {
|
||||||
len = input.read(data);
|
len = bis.read(data);
|
||||||
if (len > 0) contents.write(data, 0, len);
|
if (len > 0) contents.write(data, 0, len);
|
||||||
} while (len == data.length);
|
} while (len == data.length);
|
||||||
return contents.toString();
|
return contents.toString();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
bis.close();
|
||||||
input.close();
|
input.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user