一、前言
关于apache的ftpserver的核心源码包ftpserver-core中org.apache.ftpserver.util.IoUtils输入输出工具类,对获取缓存输入输出流getBufferedInputStream/getBufferedOutputStream/getBufferedReader/getBufferedWriter、获取唯一的文件getUniqueFile、将异常Throwable转换为字符串getStackTrace、各个输入输出通道的复制转换 copy、指定目录或文件的删除处理delete/deleteDir/deleteFile等。
二、源码说明
package org.apache.ftpserver.util;@b@@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.BufferedReader;@b@import java.io.BufferedWriter;@b@import java.io.File;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.PrintWriter;@b@import java.io.Reader;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@import java.util.Random;@b@@b@public class IoUtils@b@{@b@ private static final Random RANDOM_GEN = new Random(System.currentTimeMillis());@b@@b@ public static final BufferedInputStream getBufferedInputStream(InputStream in)@b@ {@b@ BufferedInputStream bin = null;@b@ if (in instanceof BufferedInputStream)@b@ bin = (BufferedInputStream)in;@b@ else@b@ bin = new BufferedInputStream(in);@b@@b@ return bin;@b@ }@b@@b@ public static final BufferedOutputStream getBufferedOutputStream(OutputStream out)@b@ {@b@ BufferedOutputStream bout = null;@b@ if (out instanceof BufferedOutputStream)@b@ bout = (BufferedOutputStream)out;@b@ else@b@ bout = new BufferedOutputStream(out);@b@@b@ return bout;@b@ }@b@@b@ public static final BufferedReader getBufferedReader(Reader reader)@b@ {@b@ BufferedReader buffered = null;@b@ if (reader instanceof BufferedReader)@b@ buffered = (BufferedReader)reader;@b@ else@b@ buffered = new BufferedReader(reader);@b@@b@ return buffered;@b@ }@b@@b@ public static final BufferedWriter getBufferedWriter(Writer wr)@b@ {@b@ BufferedWriter bw = null;@b@ if (wr instanceof BufferedWriter)@b@ bw = (BufferedWriter)wr;@b@ else@b@ bw = new BufferedWriter(wr);@b@@b@ return bw;@b@ }@b@@b@ public static final File getUniqueFile(File oldFile)@b@ {@b@ File newFile = oldFile;@b@ while (true) {@b@ if (!(newFile.exists()))@b@ break;@b@@b@ newFile = new File(oldFile.getAbsolutePath() + '.' + Math.abs(RANDOM_GEN.nextLong()));@b@ }@b@@b@ return newFile;@b@ }@b@@b@ public static final void close(InputStream is)@b@ {@b@ if (is != null)@b@ try {@b@ is.close();@b@ }@b@ catch (Exception ex)@b@ {@b@ }@b@ }@b@@b@ public static final void close(OutputStream os)@b@ {@b@ if (os != null)@b@ try {@b@ os.close();@b@ }@b@ catch (Exception ex)@b@ {@b@ }@b@ }@b@@b@ public static final void close(Reader rd)@b@ {@b@ if (rd != null)@b@ try {@b@ rd.close();@b@ }@b@ catch (Exception ex)@b@ {@b@ }@b@ }@b@@b@ public static final void close(Writer wr)@b@ {@b@ if (wr != null)@b@ try {@b@ wr.close();@b@ }@b@ catch (Exception ex)@b@ {@b@ }@b@ }@b@@b@ public static final String getStackTrace(Throwable ex)@b@ {@b@ String result = "";@b@ if (ex != null)@b@ try {@b@ StringWriter sw = new StringWriter();@b@ PrintWriter pw = new PrintWriter(sw);@b@ ex.printStackTrace(pw);@b@ pw.close();@b@ sw.close();@b@ result = sw.toString();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@@b@ return result;@b@ }@b@@b@ public static final void copy(Reader input, Writer output, int bufferSize)@b@ throws IOException@b@ {@b@ char[] buffer = new char[bufferSize];@b@ int n = 0;@b@ while ((n = input.read(buffer)) != -1)@b@ output.write(buffer, 0, n);@b@ }@b@@b@ public static final void copy(InputStream input, OutputStream output, int bufferSize)@b@ throws IOException@b@ {@b@ byte[] buffer = new byte[bufferSize];@b@ int n = 0;@b@ while ((n = input.read(buffer)) != -1)@b@ output.write(buffer, 0, n);@b@ }@b@@b@ public static final String readFully(Reader reader)@b@ throws IOException@b@ {@b@ StringWriter writer = new StringWriter();@b@ copy(reader, writer, 1024);@b@ return writer.toString();@b@ }@b@@b@ public static final String readFully(InputStream input)@b@ throws IOException@b@ {@b@ StringWriter writer = new StringWriter();@b@ InputStreamReader reader = new InputStreamReader(input);@b@ copy(reader, writer, 1024);@b@ return writer.toString();@b@ }@b@@b@ public static final void delete(File file) throws IOException {@b@ if (!(file.exists()))@b@ return;@b@ if (file.isDirectory())@b@ deleteDir(file);@b@ else@b@ deleteFile(file);@b@ }@b@@b@ private static final void deleteDir(File dir) throws IOException@b@ {@b@ File[] children = dir.listFiles();@b@@b@ if (children == null) {@b@ return;@b@ }@b@@b@ for (int i = 0; i < children.length; ++i) {@b@ File file = children[i];@b@ delete(file);@b@ }@b@@b@ if (!(dir.delete()))@b@ throw new IOException("Failed to delete directory: " + dir);@b@ }@b@@b@ private static final void deleteFile(File file)@b@ throws IOException@b@ {@b@ if (!(file.delete()))@b@ {@b@ if (OS.isFamilyWindows())@b@ System.gc();@b@ try@b@ {@b@ Thread.sleep(10L);@b@ } catch (InterruptedException e) {@b@ }@b@ if (!(file.delete()))@b@ throw new IOException("Failed to delete file: " + file);@b@ }@b@ }@b@}