抽象類InputStream

首先來說一下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;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章