io의 사용자 정의 InputStream
2489 단어 IO사용자 정의 Strem
package com.laien.io;
import java.io.IOException;
import java.io.InputStream;
/**
* InputStream,
*
* @author Administrator
*
*/
public class MyByteArrayInputStream extends InputStream {
protected byte[] arr; //
protected int ptr = 0; //
protected int mark=0;//
/**
* ,
* @param arr
*/
public MyByteArrayInputStream(byte[] arr) {
this.arr = arr;
}
/**
* ,
*/
@Override
public int read() throws IOException {
// , -1
return (ptr < arr.length) ? arr[ptr++] : -1;
}
/**
*
*/
@Override
public int available() throws IOException {
return arr.length - ptr;
}
@Override
public void close() throws IOException {
ptr = arr.length;
}
/**
* , reset
*/
@Override
public synchronized void mark(int readlimit) {
this.mark = readlimit;
}
@Override
public synchronized void reset() throws IOException {
if (this.mark<0 || this.mark >= arr.length) {
throw new IOException(" ");
}
ptr = mark;// mark ,
}
/**
*
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (ptr >= arr.length || len <0) {
//
return -1;
}
if (len == 0) {
return 0;
}
// len ,
len = available() < len ? available() : len;
System.arraycopy(arr, ptr, b, off, len);
ptr += len;
//
return len;
}
public static void main(String[] args) throws IOException {
//
byte[] source = new byte[10];
for (int i=0; i<source.length; i++) {
source[i] = (byte)i;
}
MyByteArrayInputStream mis = new MyByteArrayInputStream(source);
byte[] buff = new byte[4]; //
int len =0;
while(-1 != (len = mis.read(buff, 0, buff.length))) {
for (int i=0; i<len; i++) {
System.out.print(buff[i]);
}
System.out.println();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 기본 13. I/O스트림 (Stream) / 버퍼 (Buffer) / 채널 (Channel) 기반의 I/O Byte와 Character 스트림 표준 스트림 (System.in, System.out, System.err) 스트림 단방향...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.