抽象类InputStream

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41054313/article/details/89143815

首先来说一下InputStaram类是一个抽象类,不可以实例化(抽象类为什么不能实例化自行百度),java.io.InputStream类实现了Closeable接口 重写了close()方法;但实际上close方法体是空的,具体的实现方法是有其子类实现的;

再说其read方法是一个抽象方法但其重载方法以及skip方法都是通过调用该抽象方法实现自己的的功能的,子类不再重写;

java.io.InputStream类源码

package java.io;

public abstract class InputStream implements Closeable {
	private static final int MAX_SKIP_BUFFER_SIZE = 2048;

	public abstract int read() throws IOException;

	public int read(byte[] paramArrayOfByte) throws IOException {
		return read(paramArrayOfByte, 0, paramArrayOfByte.length);
	}

	public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException {
		if (paramArrayOfByte == null) {
			throw new NullPointerException();
		}
		if ((paramInt1 < 0) || (paramInt2 < 0) || (paramInt2 > paramArrayOfByte.length - paramInt1)) {
			throw new IndexOutOfBoundsException();
		}
		if (paramInt2 == 0) {
			return 0;
		}
		int i = read();
		if (i == -1) {
			return -1;
		}
		paramArrayOfByte[paramInt1] = ((byte) i);
		int j = 1;
		try {
			while (j < paramInt2) {
				i = read();
				if (i == -1) {
					break;
				}
				paramArrayOfByte[(paramInt1 + j)] = ((byte) i);
				j++;
			}
		} catch (IOException localIOException) {
		}
		return j;
	}

	public long skip(long paramLong) throws IOException {
		long l = paramLong;
		if (paramLong <= 0L) {
			return 0L;
		}
		int j = (int) Math.min(2048L, l);
		byte[] arrayOfByte = new byte[j];
		while (l > 0L) {
			int i = read(arrayOfByte, 0, (int) Math.min(j, l));
			if (i < 0) {
				break;
			}
			l -= i;
		}
		return paramLong - l;
	}

	public int available() throws IOException {
		return 0;
	}

	public void close() throws IOException {
	}

	public synchronized void mark(int paramInt) {
	}

	public synchronized void reset() throws IOException {
		throw new IOException("mark/reset not supported");
	}

	public boolean markSupported() {
		return false;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41054313/article/details/89143815