Browse Source

modify FileUtils.readFile2Str (#1535)

pull/2/head
Yelli 5 years ago committed by qiaozhanwei
parent
commit
d52a83bfcf
  1. 39
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java

39
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java

@ -152,7 +152,7 @@ public class FileUtils {
} }
bufferedReader = new BufferedReader(new StringReader(content)); bufferedReader = new BufferedReader(new StringReader(content));
bufferedWriter = new BufferedWriter(new FileWriter(distFile)); bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; char[] buf = new char[1024];
int len; int len;
while ((len = bufferedReader.read(buf)) != -1) { while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len); bufferedWriter.write(buf, 0, len);
@ -320,7 +320,7 @@ public class FileUtils {
if (file.isDirectory()) { if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory"); throw new IOException("File '" + file + "' exists but is a directory");
} }
if (file.canWrite() == false) { if (!file.canWrite()) {
throw new IOException("File '" + file + "' cannot be written to"); throw new IOException("File '" + file + "' cannot be written to");
} }
} else { } else {
@ -377,41 +377,24 @@ public class FileUtils {
throw new RuntimeException("parentDir not exist, or is not a directory:"+parentDir); throw new RuntimeException("parentDir not exist, or is not a directory:"+parentDir);
} }
File[] schemaDirs = file.listFiles(new FileFilter() { return file.listFiles(File::isDirectory);
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
else {
return false;
}
}
});
return schemaDirs;
} }
/** /**
* Get Content * Get Content
* @param inputStream input stream * @param inputStream input stream
* @return string of input stream * @return string of input stream
* @throws IOException errors
*/ */
public static String readFile2Str(InputStream inputStream) throws IOException{ public static String readFile2Str(InputStream inputStream) {
String all_content=null;
try { try {
all_content = new String(); ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream ins = inputStream; byte[] buffer = new byte[1024];
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(); int length;
byte[] str_b = new byte[1024]; while ((length= inputStream.read(buffer)) != -1) {
int i = -1; output.write(buffer,0,length);
while ((i=ins.read(str_b)) > 0) {
outputstream.write(str_b,0,i);
} }
all_content = outputstream.toString(); return output.toString();
return all_content;
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage(),e); logger.error(e.getMessage(),e);
throw new RuntimeException(e); throw new RuntimeException(e);

Loading…
Cancel
Save