首页

通过定义流工具类StreamUtil进行InputStream/OutputStream输入输出、转输入流文本为字符串数据等常用处理代码示例

标签:IO流工具类,StreamUtil,InputStream,OutputStream     发布时间:2018-08-08   

一、前言

定义输入输出流工具类StreamUtil,对InputStream/OutputStream输入输出流、Reader/Writer文本读物写出转换处理,读取输入流InputStream或文本Reader为字符串等代码示例。

二、代码示例

import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.Reader;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@@b@ @b@public class StreamUtil {@b@	@b@    private static final int DEFAULT_BUFFER_SIZE = 8192;@b@@b@    public static void io(InputStream in, OutputStream out) throws IOException {@b@        io(in, out, -1);@b@    }@b@@b@    public static void io(InputStream in, OutputStream out, int bufferSize) throws IOException {@b@        if (bufferSize == -1) {@b@            bufferSize = DEFAULT_BUFFER_SIZE;@b@        }@b@@b@        byte[] buffer = new byte[bufferSize];@b@        int amount;@b@@b@        while ((amount = in.read(buffer)) >= 0) {@b@            out.write(buffer, 0, amount);@b@        }@b@    }@b@@b@    public static void io(Reader in, Writer out) throws IOException {@b@        io(in, out, -1);@b@    }@b@@b@    public static void io(Reader in, Writer out, int bufferSize) throws IOException {@b@        if (bufferSize == -1) {@b@            bufferSize = DEFAULT_BUFFER_SIZE >> 1;@b@        }@b@@b@        char[] buffer = new char[bufferSize];@b@        int amount;@b@@b@        while ((amount = in.read(buffer)) >= 0) {@b@            out.write(buffer, 0, amount);@b@        }@b@    }@b@@b@    public static OutputStream synchronizedOutputStream(OutputStream out) {@b@        return new SynchronizedOutputStream(out);@b@    }@b@@b@    public static OutputStream synchronizedOutputStream(OutputStream out, Object lock) {@b@        return new SynchronizedOutputStream(out, lock);@b@    }@b@@b@    public static String readText(InputStream in) throws IOException {@b@        return readText(in, null, -1);@b@    }@b@@b@    public static String readText(InputStream in, String encoding) throws IOException {@b@        return readText(in, encoding, -1);@b@    }@b@@b@    public static String readText(InputStream in, String encoding, int bufferSize)@b@                                                                                  throws IOException {@b@        Reader reader = (encoding == null) ? new InputStreamReader(in) : new InputStreamReader(in,@b@            encoding);@b@@b@        return readText(reader, bufferSize);@b@    }@b@@b@    public static String readText(Reader reader) throws IOException {@b@        return readText(reader, -1);@b@    }@b@@b@    public static String readText(Reader reader, int bufferSize) throws IOException {@b@        StringWriter writer = new StringWriter();@b@@b@        io(reader, writer, bufferSize);@b@        return writer.toString();@b@    }@b@@b@    private static class SynchronizedOutputStream extends OutputStream {@b@        private OutputStream out;@b@        private Object       lock;@b@@b@        SynchronizedOutputStream(OutputStream out) {@b@            this(out, out);@b@        }@b@@b@        SynchronizedOutputStream(OutputStream out, Object lock) {@b@            this.out = out;@b@            this.lock = lock;@b@        }@b@@b@        public void write(int datum) throws IOException {@b@            synchronized (lock) {@b@                out.write(datum);@b@            }@b@        }@b@@b@        public void write(byte[] data) throws IOException {@b@            synchronized (lock) {@b@                out.write(data);@b@            }@b@        }@b@@b@        public void write(byte[] data, int offset, int length) throws IOException {@b@            synchronized (lock) {@b@                out.write(data, offset, length);@b@            }@b@        }@b@@b@        public void flush() throws IOException {@b@            synchronized (lock) {@b@                out.flush();@b@            }@b@        }@b@@b@        public void close() throws IOException {@b@            synchronized (lock) {@b@                out.close();@b@            }@b@        }@b@    }@b@}