You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2 KiB

5 years ago
package ${basePackage}.frame.utils;
import java.io.*;
public class FileUtil {
public static String readFileToString(File file) throws IOException {
byte[] bytes = readFileToByteArray(file);
return new String(bytes, "UTF-8");
}
5 years ago
public static byte[] readFileToByteArray(File file) throws IOException {
InputStream in = openInputStream(file);
Throwable var2 = null;
byte[] var5;
try {
long fileLength = file.length();
var5 = fileLength > 0L ? toByteArray(in, fileLength) : toByteArray(in);
} catch (Throwable var14) {
var2 = var14;
throw var14;
} finally {
if (in != null) {
if (var2 != null) {
try {
in.close();
} catch (Throwable var13) {
var2.addSuppressed(var13);
}
} else {
in.close();
}
}
}
return var5;
}
public static FileInputStream openInputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
} else if (!file.canRead()) {
throw new IOException("File '" + file + "' cannot be read");
} else {
return new FileInputStream(file);
}
} else {
throw new FileNotFoundException("File '" + file + "' does not exist");
}
}
public static byte[] toByteArray(InputStream input, long size) throws IOException {
if (size > 2147483647L) {
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);
} else {
return toByteArray(input, (int) size);
}
}
public static byte[] toByteArray(InputStream input, int size) throws IOException {
if(size < 0) {
throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
} else if(size == 0) {
return new byte[0];
} else {
byte[] data = new byte[size];
int offset;
int read;
for(offset = 0; offset < size && (read = input.read(data, offset, size - offset)) != -1; offset += read) {
;
}
if(offset != size) {
throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size);
} else {
return data;
}
}
}
5 years ago
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Throwable var2 = null;
byte[] var3;
try {
copy((InputStream) input, (OutputStream) output);
var3 = output.toByteArray();
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (output != null) {
if (var2 != null) {
try {
output.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
output.close();
}
}
}
return var3;
}
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
return count > 2147483647L ? -1 : (int) count;
}
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
return copy(input, output, 4096);
}
public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
return copyLarge(input, output, new byte[bufferSize]);
}
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
long count;
int n;
for (count = 0L; -1 != (n = input.read(buffer)); count += (long) n) {
output.write(buffer, 0, n);
}
return count;
}
}

Powered by TurnKey Linux.