首页

关于apache的commons-io包IOUtils输入输出工具类对输入流转换、写出文件流及输入输出复制、内容匹配等操作

标签:IOUtils,输入输出工具类,apache,commons-io     发布时间:2018-02-13   

一、前言

关于apachecommons-io包org.apache.commons.io.IOUtils输入输出工具类,对输入输出或套接字进行关闭closeQuietly、输入流转换二进制toByteArray、输入流转换为字符串toString、字符串转换输入流toInputStream、写出二进制数据到输出流文件write、从不同输出流文件复制到输出流或文件copy等。

二、源码说明

1.IOUtils工具类

package org.apache.commons.io;@b@@b@import java.io.BufferedInputStream;@b@import java.io.BufferedReader;@b@import java.io.ByteArrayInputStream;@b@import java.io.CharArrayWriter;@b@import java.io.Closeable;@b@import java.io.EOFException;@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.OutputStreamWriter;@b@import java.io.PrintWriter;@b@import java.io.Reader;@b@import java.io.Writer;@b@import java.net.Socket;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.List;@b@import org.apache.commons.io.output.ByteArrayOutputStream;@b@import org.apache.commons.io.output.StringBuilderWriter;@b@@b@public class IOUtils@b@{@b@  public static final char DIR_SEPARATOR_UNIX = 47;@b@  public static final char DIR_SEPARATOR_WINDOWS = 92;@b@  public static final char DIR_SEPARATOR = File.separatorChar;@b@  public static final String LINE_SEPARATOR_UNIX = "\n";@b@  public static final String LINE_SEPARATOR_WINDOWS = "\r\n";@b@  public static final String LINE_SEPARATOR;@b@  private static final int DEFAULT_BUFFER_SIZE = 4096;@b@  private static final int SKIP_BUFFER_SIZE = 2048;@b@  private static char[] SKIP_CHAR_BUFFER;@b@  private static byte[] SKIP_BYTE_BUFFER;@b@@b@  public static void closeQuietly(Reader input)@b@  {@b@    closeQuietly(input);@b@  }@b@@b@  public static void closeQuietly(Writer output)@b@  {@b@    closeQuietly(output);@b@  }@b@@b@  public static void closeQuietly(InputStream input)@b@  {@b@    closeQuietly(input);@b@  }@b@@b@  public static void closeQuietly(OutputStream output)@b@  {@b@    closeQuietly(output);@b@  }@b@@b@  public static void closeQuietly(Closeable closeable)@b@  {@b@    try@b@    {@b@      if (closeable != null)@b@        closeable.close();@b@    }@b@    catch (IOException ioe)@b@    {@b@    }@b@  }@b@@b@  public static void closeQuietly(Socket sock)@b@  {@b@    if (sock != null)@b@      try {@b@        sock.close();@b@      }@b@      catch (IOException ioe)@b@      {@b@      }@b@  }@b@@b@  public static InputStream toBufferedInputStream(InputStream input)@b@    throws IOException@b@  {@b@    return ByteArrayOutputStream.toBufferedInputStream(input);@b@  }@b@@b@  public static byte[] toByteArray(InputStream input)@b@    throws IOException@b@  {@b@    ByteArrayOutputStream output = new ByteArrayOutputStream();@b@    copy(input, output);@b@    return output.toByteArray();@b@  }@b@@b@  public static byte[] toByteArray(Reader input)@b@    throws IOException@b@  {@b@    ByteArrayOutputStream output = new ByteArrayOutputStream();@b@    copy(input, output);@b@    return output.toByteArray();@b@  }@b@@b@  public static byte[] toByteArray(Reader input, String encoding)@b@    throws IOException@b@  {@b@    ByteArrayOutputStream output = new ByteArrayOutputStream();@b@    copy(input, output, encoding);@b@    return output.toByteArray();@b@  }@b@@b@  @Deprecated@b@  public static byte[] toByteArray(String input)@b@    throws IOException@b@  {@b@    return input.getBytes();@b@  }@b@@b@  public static char[] toCharArray(InputStream is)@b@    throws IOException@b@  {@b@    CharArrayWriter output = new CharArrayWriter();@b@    copy(is, output);@b@    return output.toCharArray();@b@  }@b@@b@  public static char[] toCharArray(InputStream is, String encoding)@b@    throws IOException@b@  {@b@    CharArrayWriter output = new CharArrayWriter();@b@    copy(is, output, encoding);@b@    return output.toCharArray();@b@  }@b@@b@  public static char[] toCharArray(Reader input)@b@    throws IOException@b@  {@b@    CharArrayWriter sw = new CharArrayWriter();@b@    copy(input, sw);@b@    return sw.toCharArray();@b@  }@b@@b@  public static String toString(InputStream input)@b@    throws IOException@b@  {@b@    StringBuilderWriter sw = new StringBuilderWriter();@b@    copy(input, sw);@b@    return sw.toString();@b@  }@b@@b@  public static String toString(InputStream input, String encoding)@b@    throws IOException@b@  {@b@    StringBuilderWriter sw = new StringBuilderWriter();@b@    copy(input, sw, encoding);@b@    return sw.toString();@b@  }@b@@b@  public static String toString(Reader input)@b@    throws IOException@b@  {@b@    StringBuilderWriter sw = new StringBuilderWriter();@b@    copy(input, sw);@b@    return sw.toString();@b@  }@b@@b@  @Deprecated@b@  public static String toString(byte[] input)@b@    throws IOException@b@  {@b@    return new String(input);@b@  }@b@@b@  @Deprecated@b@  public static String toString(byte[] input, String encoding)@b@    throws IOException@b@  {@b@    if (encoding == null)@b@      return new String(input);@b@@b@    return new String(input, encoding);@b@  }@b@@b@  public static List<String> readLines(InputStream input)@b@    throws IOException@b@  {@b@    InputStreamReader reader = new InputStreamReader(input);@b@    return readLines(reader);@b@  }@b@@b@  public static List<String> readLines(InputStream input, String encoding)@b@    throws IOException@b@  {@b@    if (encoding == null)@b@      return readLines(input);@b@@b@    InputStreamReader reader = new InputStreamReader(input, encoding);@b@    return readLines(reader);@b@  }@b@@b@  public static List<String> readLines(Reader input)@b@    throws IOException@b@  {@b@    BufferedReader reader = new BufferedReader(input);@b@    List list = new ArrayList();@b@    String line = reader.readLine();@b@    while (line != null) {@b@      list.add(line);@b@      line = reader.readLine();@b@    }@b@    return list;@b@  }@b@@b@  public static LineIterator lineIterator(Reader reader)@b@  {@b@    return new LineIterator(reader);@b@  }@b@@b@  public static LineIterator lineIterator(InputStream input, String encoding)@b@    throws IOException@b@  {@b@    Reader reader = null;@b@    if (encoding == null)@b@      reader = new InputStreamReader(input);@b@    else@b@      reader = new InputStreamReader(input, encoding);@b@@b@    return new LineIterator(reader);@b@  }@b@@b@  public static InputStream toInputStream(CharSequence input)@b@  {@b@    return toInputStream(input.toString());@b@  }@b@@b@  public static InputStream toInputStream(CharSequence input, String encoding)@b@    throws IOException@b@  {@b@    return toInputStream(input.toString(), encoding);@b@  }@b@@b@  public static InputStream toInputStream(String input)@b@  {@b@    byte[] bytes = input.getBytes();@b@    return new ByteArrayInputStream(bytes);@b@  }@b@@b@  public static InputStream toInputStream(String input, String encoding)@b@    throws IOException@b@  {@b@    byte[] bytes = (encoding != null) ? input.getBytes(encoding) : input.getBytes();@b@    return new ByteArrayInputStream(bytes);@b@  }@b@@b@  public static void write(byte[] data, OutputStream output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data);@b@  }@b@@b@  public static void write(byte[] data, Writer output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(new String(data));@b@  }@b@@b@  public static void write(byte[] data, Writer output, String encoding)@b@    throws IOException@b@  {@b@    if (data != null)@b@      if (encoding == null)@b@        write(data, output);@b@      else@b@        output.write(new String(data, encoding));@b@  }@b@@b@  public static void write(char[] data, Writer output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data);@b@  }@b@@b@  public static void write(char[] data, OutputStream output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(new String(data).getBytes());@b@  }@b@@b@  public static void write(char[] data, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    if (data != null)@b@      if (encoding == null)@b@        write(data, output);@b@      else@b@        output.write(new String(data).getBytes(encoding));@b@  }@b@@b@  public static void write(CharSequence data, Writer output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      write(data.toString(), output);@b@  }@b@@b@  public static void write(CharSequence data, OutputStream output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      write(data.toString(), output);@b@  }@b@@b@  public static void write(CharSequence data, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    if (data != null)@b@      write(data.toString(), output, encoding);@b@  }@b@@b@  public static void write(String data, Writer output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data);@b@  }@b@@b@  public static void write(String data, OutputStream output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data.getBytes());@b@  }@b@@b@  public static void write(String data, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    if (data != null)@b@      if (encoding == null)@b@        write(data, output);@b@      else@b@        output.write(data.getBytes(encoding));@b@  }@b@@b@  @Deprecated@b@  public static void write(StringBuffer data, Writer output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data.toString());@b@  }@b@@b@  @Deprecated@b@  public static void write(StringBuffer data, OutputStream output)@b@    throws IOException@b@  {@b@    if (data != null)@b@      output.write(data.toString().getBytes());@b@  }@b@@b@  @Deprecated@b@  public static void write(StringBuffer data, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    if (data != null)@b@      if (encoding == null)@b@        write(data, output);@b@      else@b@        output.write(data.toString().getBytes(encoding));@b@  }@b@@b@  public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output)@b@    throws IOException@b@  {@b@    if (lines == null)@b@      return;@b@@b@    if (lineEnding == null)@b@      lineEnding = LINE_SEPARATOR;@b@@b@    for (Iterator i$ = lines.iterator(); i$.hasNext(); ) { Object line = i$.next();@b@      if (line != null)@b@        output.write(line.toString().getBytes());@b@@b@      output.write(lineEnding.getBytes());@b@    }@b@  }@b@@b@  public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    Iterator i$;@b@    if (encoding == null) {@b@      writeLines(lines, lineEnding, output);@b@    } else {@b@      if (lines == null)@b@        return;@b@@b@      if (lineEnding == null)@b@        lineEnding = LINE_SEPARATOR;@b@@b@      for (i$ = lines.iterator(); i$.hasNext(); ) { Object line = i$.next();@b@        if (line != null)@b@          output.write(line.toString().getBytes(encoding));@b@@b@        output.write(lineEnding.getBytes(encoding));@b@      }@b@    }@b@  }@b@@b@  public static void writeLines(Collection<?> lines, String lineEnding, Writer writer)@b@    throws IOException@b@  {@b@    if (lines == null)@b@      return;@b@@b@    if (lineEnding == null)@b@      lineEnding = LINE_SEPARATOR;@b@@b@    for (Iterator i$ = lines.iterator(); i$.hasNext(); ) { Object line = i$.next();@b@      if (line != null)@b@        writer.write(line.toString());@b@@b@      writer.write(lineEnding);@b@    }@b@  }@b@@b@  public static int copy(InputStream input, OutputStream output)@b@    throws IOException@b@  {@b@    long count = copyLarge(input, output);@b@    if (count > 2147483647L)@b@      return -1;@b@@b@    return (int)count;@b@  }@b@@b@  public static long copyLarge(InputStream input, OutputStream output)@b@    throws IOException@b@  {@b@    byte[] buffer = new byte[4096];@b@    long count = 0L;@b@    int n = 0;@b@    while (-1 != (n = input.read(buffer))) {@b@      output.write(buffer, 0, n);@b@      count += n;@b@    }@b@    return count;@b@  }@b@@b@  public static void copy(InputStream input, Writer output)@b@    throws IOException@b@  {@b@    InputStreamReader in = new InputStreamReader(input);@b@    copy(in, output);@b@  }@b@@b@  public static void copy(InputStream input, Writer output, String encoding)@b@    throws IOException@b@  {@b@    if (encoding == null) {@b@      copy(input, output);@b@    } else {@b@      InputStreamReader in = new InputStreamReader(input, encoding);@b@      copy(in, output);@b@    }@b@  }@b@@b@  public static int copy(Reader input, Writer output)@b@    throws IOException@b@  {@b@    long count = copyLarge(input, output);@b@    if (count > 2147483647L)@b@      return -1;@b@@b@    return (int)count;@b@  }@b@@b@  public static long copyLarge(Reader input, Writer output)@b@    throws IOException@b@  {@b@    char[] buffer = new char[4096];@b@    long count = 0L;@b@    int n = 0;@b@    while (-1 != (n = input.read(buffer))) {@b@      output.write(buffer, 0, n);@b@      count += n;@b@    }@b@    return count;@b@  }@b@@b@  public static void copy(Reader input, OutputStream output)@b@    throws IOException@b@  {@b@    OutputStreamWriter out = new OutputStreamWriter(output);@b@    copy(input, out);@b@@b@    out.flush();@b@  }@b@@b@  public static void copy(Reader input, OutputStream output, String encoding)@b@    throws IOException@b@  {@b@    if (encoding == null) {@b@      copy(input, output);@b@    } else {@b@      OutputStreamWriter out = new OutputStreamWriter(output, encoding);@b@      copy(input, out);@b@@b@      out.flush();@b@    }@b@  }@b@@b@  public static boolean contentEquals(InputStream input1, InputStream input2)@b@    throws IOException@b@  {@b@    if (!(input1 instanceof BufferedInputStream))@b@      input1 = new BufferedInputStream(input1);@b@@b@    if (!(input2 instanceof BufferedInputStream)) {@b@      input2 = new BufferedInputStream(input2);@b@    }@b@@b@    int ch = input1.read();@b@    while (-1 != ch) {@b@      ch2 = input2.read();@b@      if (ch != ch2)@b@        return false;@b@@b@      ch = input1.read();@b@    }@b@@b@    int ch2 = input2.read();@b@    return (ch2 == -1);@b@  }@b@@b@  public static boolean contentEquals(Reader input1, Reader input2)@b@    throws IOException@b@  {@b@    if (!(input1 instanceof BufferedReader))@b@      input1 = new BufferedReader(input1);@b@@b@    if (!(input2 instanceof BufferedReader)) {@b@      input2 = new BufferedReader(input2);@b@    }@b@@b@    int ch = input1.read();@b@    while (-1 != ch) {@b@      ch2 = input2.read();@b@      if (ch != ch2)@b@        return false;@b@@b@      ch = input1.read();@b@    }@b@@b@    int ch2 = input2.read();@b@    return (ch2 == -1);@b@  }@b@@b@  public static long skip(InputStream input, long toSkip)@b@    throws IOException@b@  {@b@    if (toSkip < 0L) {@b@      throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);@b@    }@b@@b@    if (SKIP_BYTE_BUFFER == null)@b@      SKIP_BYTE_BUFFER = new byte[2048];@b@@b@    long remain = toSkip;@b@    while (remain > 0L) {@b@      long n = input.read(SKIP_BYTE_BUFFER, 0, (int)Math.min(remain, 2048L));@b@      if (n < 0L)@b@        break;@b@@b@      remain -= n;@b@    }@b@    return (toSkip - remain);@b@  }@b@@b@  public static long skip(Reader input, long toSkip)@b@    throws IOException@b@  {@b@    if (toSkip < 0L) {@b@      throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);@b@    }@b@@b@    if (SKIP_CHAR_BUFFER == null)@b@      SKIP_CHAR_BUFFER = new char[2048];@b@@b@    long remain = toSkip;@b@    while (remain > 0L) {@b@      long n = input.read(SKIP_CHAR_BUFFER, 0, (int)Math.min(remain, 2048L));@b@      if (n < 0L)@b@        break;@b@@b@      remain -= n;@b@    }@b@    return (toSkip - remain);@b@  }@b@@b@  public static void skipFully(InputStream input, long toSkip)@b@    throws IOException@b@  {@b@    if (toSkip < 0L)@b@      throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);@b@@b@    long skipped = skip(input, toSkip);@b@    if (skipped != toSkip)@b@      throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);@b@  }@b@@b@  public static void skipFully(Reader input, long toSkip)@b@    throws IOException@b@  {@b@    long skipped = skip(input, toSkip);@b@    if (skipped != toSkip)@b@      throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);@b@  }@b@@b@  static@b@  {@b@    StringBuilderWriter buf = new StringBuilderWriter(4);@b@    PrintWriter out = new PrintWriter(buf);@b@    out.println();@b@    LINE_SEPARATOR = buf.toString();@b@    out.close();@b@  }@b@}

2.ByteArrayOutputStream

package org.apache.commons.io.output;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.SequenceInputStream;@b@import java.io.UnsupportedEncodingException;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.List;@b@import org.apache.commons.io.input.ClosedInputStream;@b@@b@public class ByteArrayOutputStream extends OutputStream@b@{@b@  private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];@b@  private final List<byte[]> buffers;@b@  private int currentBufferIndex;@b@  private int filledBufferSum;@b@  private byte[] currentBuffer;@b@  private int count;@b@@b@  public ByteArrayOutputStream()@b@  {@b@    this(1024);@b@  }@b@@b@  public ByteArrayOutputStream(int size)@b@  {@b@    this.buffers = new ArrayList();@b@@b@    if (size < 0) {@b@      throw new IllegalArgumentException("Negative initial size: " + size);@b@    }@b@@b@    synchronized (this) {@b@      needNewBuffer(size);@b@    }@b@  }@b@@b@  private void needNewBuffer(int newcount)@b@  {@b@    if (this.currentBufferIndex < this.buffers.size() - 1)@b@    {@b@      this.filledBufferSum += this.currentBuffer.length;@b@@b@      this.currentBufferIndex += 1;@b@      this.currentBuffer = ((byte[])this.buffers.get(this.currentBufferIndex));@b@    }@b@    else@b@    {@b@      int newBufferSize;@b@      if (this.currentBuffer == null) {@b@        newBufferSize = newcount;@b@        this.filledBufferSum = 0;@b@      } else {@b@        newBufferSize = Math.max(this.currentBuffer.length << 1, newcount - this.filledBufferSum);@b@@b@        this.filledBufferSum += this.currentBuffer.length;@b@      }@b@@b@      this.currentBufferIndex += 1;@b@      this.currentBuffer = new byte[newBufferSize];@b@      this.buffers.add(this.currentBuffer);@b@    }@b@  }@b@@b@  public void write(byte[] b, int off, int len)@b@  {@b@    if ((off < 0) || (off > b.length) || (len < 0) || (off + len > b.length) || (off + len < 0))@b@    {@b@      throw new IndexOutOfBoundsException(); }@b@    if (len == 0)@b@      return;@b@@b@    synchronized (this) {@b@      int newcount = this.count + len;@b@      int remaining = len;@b@      int inBufferPos = this.count - this.filledBufferSum;@b@      while (remaining > 0) {@b@        int part = Math.min(remaining, this.currentBuffer.length - inBufferPos);@b@        System.arraycopy(b, off + len - remaining, this.currentBuffer, inBufferPos, part);@b@        remaining -= part;@b@        if (remaining > 0) {@b@          needNewBuffer(newcount);@b@          inBufferPos = 0;@b@        }@b@      }@b@      this.count = newcount;@b@    }@b@  }@b@@b@  public synchronized void write(int b)@b@  {@b@    int inBufferPos = this.count - this.filledBufferSum;@b@    if (inBufferPos == this.currentBuffer.length) {@b@      needNewBuffer(this.count + 1);@b@      inBufferPos = 0;@b@    }@b@    this.currentBuffer[inBufferPos] = (byte)b;@b@    this.count += 1;@b@  }@b@@b@  public synchronized int write(InputStream in)@b@    throws IOException@b@  {@b@    int readCount = 0;@b@    int inBufferPos = this.count - this.filledBufferSum;@b@    int n = in.read(this.currentBuffer, inBufferPos, this.currentBuffer.length - inBufferPos);@b@    while (n != -1) {@b@      readCount += n;@b@      inBufferPos += n;@b@      this.count += n;@b@      if (inBufferPos == this.currentBuffer.length) {@b@        needNewBuffer(this.currentBuffer.length);@b@        inBufferPos = 0;@b@      }@b@      n = in.read(this.currentBuffer, inBufferPos, this.currentBuffer.length - inBufferPos);@b@    }@b@    return readCount;@b@  }@b@@b@  public synchronized int size()@b@  {@b@    return this.count;@b@  }@b@@b@  public void close()@b@    throws IOException@b@  {@b@  }@b@@b@  public synchronized void reset()@b@  {@b@    this.count = 0;@b@    this.filledBufferSum = 0;@b@    this.currentBufferIndex = 0;@b@    this.currentBuffer = ((byte[])this.buffers.get(this.currentBufferIndex));@b@  }@b@@b@  public synchronized void writeTo(OutputStream out)@b@    throws IOException@b@  {@b@    int remaining = this.count;@b@    for (byte[] buf : this.buffers) {@b@      int c = Math.min(buf.length, remaining);@b@      out.write(buf, 0, c);@b@      remaining -= c;@b@      if (remaining == 0)@b@        return;@b@    }@b@  }@b@@b@  public static InputStream toBufferedInputStream(InputStream input)@b@    throws IOException@b@  {@b@    ByteArrayOutputStream output = new ByteArrayOutputStream();@b@    output.write(input);@b@    return output.toBufferedInputStream();@b@  }@b@@b@  private InputStream toBufferedInputStream()@b@  {@b@    int remaining = this.count;@b@    if (remaining == 0)@b@      return new ClosedInputStream();@b@@b@    List list = new ArrayList(this.buffers.size());@b@    for (byte[] buf : this.buffers) {@b@      int c = Math.min(buf.length, remaining);@b@      list.add(new ByteArrayInputStream(buf, 0, c));@b@      remaining -= c;@b@      if (remaining == 0)@b@        break;@b@    }@b@@b@    return new SequenceInputStream(Collections.enumeration(list));@b@  }@b@@b@  public synchronized byte[] toByteArray()@b@  {@b@    int remaining = this.count;@b@    if (remaining == 0)@b@      return EMPTY_BYTE_ARRAY;@b@@b@    byte[] newbuf = new byte[remaining];@b@    int pos = 0;@b@    for (byte[] buf : this.buffers) {@b@      int c = Math.min(buf.length, remaining);@b@      System.arraycopy(buf, 0, newbuf, pos, c);@b@      pos += c;@b@      remaining -= c;@b@      if (remaining == 0)@b@        break;@b@    }@b@@b@    return newbuf;@b@  }@b@@b@  public String toString()@b@  {@b@    return new String(toByteArray());@b@  }@b@@b@  public String toString(String enc)@b@    throws UnsupportedEncodingException@b@  {@b@    return new String(toByteArray(), enc);@b@  }@b@}

3.StringBuilderWriter

package org.apache.commons.io.output;@b@@b@import java.io.Serializable;@b@import java.io.Writer;@b@@b@public class StringBuilderWriter extends Writer@b@  implements Serializable@b@{@b@  private final StringBuilder builder;@b@@b@  public StringBuilderWriter()@b@  {@b@    this.builder = new StringBuilder();@b@  }@b@@b@  public StringBuilderWriter(int capacity)@b@  {@b@    this.builder = new StringBuilder(capacity);@b@  }@b@@b@  public StringBuilderWriter(StringBuilder builder)@b@  {@b@    this.builder = new StringBuilder();@b@  }@b@@b@  public Writer append(char value)@b@  {@b@    this.builder.append(value);@b@    return this;@b@  }@b@@b@  public Writer append(CharSequence value)@b@  {@b@    this.builder.append(value);@b@    return this;@b@  }@b@@b@  public Writer append(CharSequence value, int start, int end)@b@  {@b@    this.builder.append(value, start, end);@b@    return this;@b@  }@b@@b@  public void close()@b@  {@b@  }@b@@b@  public void flush()@b@  {@b@  }@b@@b@  public void write(String value)@b@  {@b@    if (value != null)@b@      this.builder.append(value);@b@  }@b@@b@  public void write(char[] value, int offset, int length)@b@  {@b@    if (value != null)@b@      this.builder.append(value, offset, length);@b@  }@b@@b@  public StringBuilder getBuilder()@b@  {@b@    return this.builder;@b@  }@b@@b@  public String toString()@b@  {@b@    return this.builder.toString();@b@  }@b@}
<<热门下载>>