一、前言
关于apache的commons-io源码包org.apache.commons.io.input.BrokenInputStream、org.apache.commons.io.input.NullReader、org.apache.commons.io.ClassLoaderObjectInputStream、org.apache.commons.io.NullReader等根据场景自定义方式,详情如下。
二、源码说明
1.BrokenInputStream类
package org.apache.commons.io.input;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@@b@public class BrokenInputStream extends InputStream@b@{@b@ private final IOException exception;@b@@b@ public BrokenInputStream(IOException exception)@b@ {@b@ this.exception = exception;@b@ }@b@@b@ public BrokenInputStream()@b@ {@b@ this(new IOException("Broken input stream"));@b@ }@b@@b@ public int read()@b@ throws IOException@b@ {@b@ throw this.exception;@b@ }@b@@b@ public int available()@b@ throws IOException@b@ {@b@ throw this.exception;@b@ }@b@@b@ public long skip(long n)@b@ throws IOException@b@ {@b@ throw this.exception;@b@ }@b@@b@ public void reset()@b@ throws IOException@b@ {@b@ throw this.exception;@b@ }@b@@b@ public void close()@b@ throws IOException@b@ {@b@ throw this.exception;@b@ }@b@}
2.ClassLoaderObjectInputStream类
package org.apache.commons.io.input;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectStreamClass;@b@import java.io.StreamCorruptedException;@b@import java.lang.reflect.Proxy;@b@@b@public class ClassLoaderObjectInputStream extends ObjectInputStream@b@{@b@ private final ClassLoader classLoader;@b@@b@ public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream inputStream)@b@ throws IOException, StreamCorruptedException@b@ {@b@ super(inputStream);@b@ this.classLoader = classLoader;@b@ }@b@@b@ protected Class<?> resolveClass(ObjectStreamClass objectStreamClass)@b@ throws IOException, ClassNotFoundException@b@ {@b@ Class clazz = Class.forName(objectStreamClass.getName(), false, this.classLoader);@b@@b@ if (clazz != null)@b@ {@b@ return clazz;@b@ }@b@@b@ return super.resolveClass(objectStreamClass);@b@ }@b@@b@ protected Class<?> resolveProxyClass(String[] interfaces)@b@ throws IOException, ClassNotFoundException@b@ {@b@ Class[] interfaceClasses = new Class[interfaces.length];@b@ for (int i = 0; i < interfaces.length; ++i)@b@ interfaceClasses[i] = Class.forName(interfaces[i], false, this.classLoader);@b@ try@b@ {@b@ return Proxy.getProxyClass(this.classLoader, interfaceClasses); } catch (IllegalArgumentException e) {@b@ }@b@ return super.resolveProxyClass(interfaces);@b@ }@b@}
3.NullInputStream/NullReader等
package org.apache.commons.io.input;@b@@b@import java.io.EOFException;@b@import java.io.IOException;@b@import java.io.InputStream;@b@@b@public class NullInputStream extends InputStream@b@{@b@ private final long size;@b@ private long position;@b@ private long mark;@b@ private long readlimit;@b@ private boolean eof;@b@ private final boolean throwEofException;@b@ private final boolean markSupported;@b@@b@ public NullInputStream(long size)@b@ {@b@ this(size, true, false);@b@ }@b@@b@ public NullInputStream(long size, boolean markSupported, boolean throwEofException)@b@ {@b@ this.mark = -1L;@b@@b@ this.size = size;@b@ this.markSupported = markSupported;@b@ this.throwEofException = throwEofException;@b@ }@b@@b@ public long getPosition()@b@ {@b@ return this.position;@b@ }@b@@b@ public long getSize()@b@ {@b@ return this.size;@b@ }@b@@b@ public int available()@b@ {@b@ long avail = this.size - this.position;@b@ if (avail <= 0L)@b@ return 0;@b@ if (avail > 2147483647L)@b@ return 2147483647;@b@@b@ return (int)avail;@b@ }@b@@b@ public void close()@b@ throws IOException@b@ {@b@ this.eof = false;@b@ this.position = 0L;@b@ this.mark = -1L;@b@ }@b@@b@ public synchronized void mark(int readlimit)@b@ {@b@ if (!(this.markSupported))@b@ throw new UnsupportedOperationException("Mark not supported");@b@@b@ this.mark = this.position;@b@ this.readlimit = readlimit;@b@ }@b@@b@ public boolean markSupported()@b@ {@b@ return this.markSupported;@b@ }@b@@b@ public int read()@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Read after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += 1L;@b@ return processByte();@b@ }@b@@b@ public int read(byte[] bytes)@b@ throws IOException@b@ {@b@ return read(bytes, 0, bytes.length);@b@ }@b@@b@ public int read(byte[] bytes, int offset, int length)@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Read after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += length;@b@ int returnLength = length;@b@ if (this.position > this.size) {@b@ returnLength = length - (int)(this.position - this.size);@b@ this.position = this.size;@b@ }@b@ processBytes(bytes, offset, returnLength);@b@ return returnLength;@b@ }@b@@b@ public synchronized void reset()@b@ throws IOException@b@ {@b@ if (!(this.markSupported))@b@ throw new UnsupportedOperationException("Mark not supported");@b@@b@ if (this.mark < 0L)@b@ throw new IOException("No position has been marked");@b@@b@ if (this.position > this.mark + this.readlimit) {@b@ throw new IOException("Marked position [" + this.mark + "] is no longer valid - passed the read limit [" + this.readlimit + "]");@b@ }@b@@b@ this.position = this.mark;@b@ this.eof = false;@b@ }@b@@b@ public long skip(long numberOfBytes)@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Skip after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += numberOfBytes;@b@ long returnLength = numberOfBytes;@b@ if (this.position > this.size) {@b@ returnLength = numberOfBytes - this.position - this.size;@b@ this.position = this.size;@b@ }@b@ return returnLength;@b@ }@b@@b@ protected int processByte()@b@ {@b@ return 0;@b@ }@b@@b@ protected void processBytes(byte[] bytes, int offset, int length)@b@ {@b@ }@b@@b@ private int doEndOfFile()@b@ throws EOFException@b@ {@b@ this.eof = true;@b@ if (this.throwEofException)@b@ throw new EOFException();@b@@b@ return -1;@b@ }@b@}
package org.apache.commons.io.input;@b@@b@import java.io.EOFException;@b@import java.io.IOException;@b@import java.io.Reader;@b@@b@public class NullReader extends Reader@b@{@b@ private final long size;@b@ private long position;@b@ private long mark;@b@ private long readlimit;@b@ private boolean eof;@b@ private final boolean throwEofException;@b@ private final boolean markSupported;@b@@b@ public NullReader(long size)@b@ {@b@ this(size, true, false);@b@ }@b@@b@ public NullReader(long size, boolean markSupported, boolean throwEofException)@b@ {@b@ this.mark = -1L;@b@@b@ this.size = size;@b@ this.markSupported = markSupported;@b@ this.throwEofException = throwEofException;@b@ }@b@@b@ public long getPosition()@b@ {@b@ return this.position;@b@ }@b@@b@ public long getSize()@b@ {@b@ return this.size;@b@ }@b@@b@ public void close()@b@ throws IOException@b@ {@b@ this.eof = false;@b@ this.position = 0L;@b@ this.mark = -1L;@b@ }@b@@b@ public synchronized void mark(int readlimit)@b@ {@b@ if (!(this.markSupported))@b@ throw new UnsupportedOperationException("Mark not supported");@b@@b@ this.mark = this.position;@b@ this.readlimit = readlimit;@b@ }@b@@b@ public boolean markSupported()@b@ {@b@ return this.markSupported;@b@ }@b@@b@ public int read()@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Read after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += 1L;@b@ return processChar();@b@ }@b@@b@ public int read(char[] chars)@b@ throws IOException@b@ {@b@ return read(chars, 0, chars.length);@b@ }@b@@b@ public int read(char[] chars, int offset, int length)@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Read after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += length;@b@ int returnLength = length;@b@ if (this.position > this.size) {@b@ returnLength = length - (int)(this.position - this.size);@b@ this.position = this.size;@b@ }@b@ processChars(chars, offset, returnLength);@b@ return returnLength;@b@ }@b@@b@ public synchronized void reset()@b@ throws IOException@b@ {@b@ if (!(this.markSupported))@b@ throw new UnsupportedOperationException("Mark not supported");@b@@b@ if (this.mark < 0L)@b@ throw new IOException("No position has been marked");@b@@b@ if (this.position > this.mark + this.readlimit) {@b@ throw new IOException("Marked position [" + this.mark + "] is no longer valid - passed the read limit [" + this.readlimit + "]");@b@ }@b@@b@ this.position = this.mark;@b@ this.eof = false;@b@ }@b@@b@ public long skip(long numberOfChars)@b@ throws IOException@b@ {@b@ if (this.eof)@b@ throw new IOException("Skip after end of file");@b@@b@ if (this.position == this.size)@b@ return doEndOfFile();@b@@b@ this.position += numberOfChars;@b@ long returnLength = numberOfChars;@b@ if (this.position > this.size) {@b@ returnLength = numberOfChars - this.position - this.size;@b@ this.position = this.size;@b@ }@b@ return returnLength;@b@ }@b@@b@ protected int processChar()@b@ {@b@ return 0;@b@ }@b@@b@ protected void processChars(char[] chars, int offset, int length)@b@ {@b@ }@b@@b@ private int doEndOfFile()@b@ throws EOFException@b@ {@b@ this.eof = true;@b@ if (this.throwEofException)@b@ throw new EOFException();@b@@b@ return -1;@b@ }@b@}