标签:nbsp pac import exce available OLE math 数据 参数
这个类是利用内存数组去存储,底层传输数据利用Systm.arrycopy
package io;
import java.io.IOException;
import java.util.Arrays;
/**
* @author LH
* 这个是利用内存数组存储byte数组
*/
public class LHByteArrayInputStream extends LHInputStream {
/**
* 内存数组
*/
protected byte buf[];
/**
* 位置参数
*/
protected int pos;
/**
* 标记参数
*/
protected int mark;
/**
* 个数参数
*/
protected int count;
/**
* 单个参数构造函数
*
* @param buf
*/
public LHByteArrayInputStream(byte[] buf) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
}
/**
* 多参数构造函数
*
* @param buf
* @param offset 偏移
* @param length 长度
*/
public LHByteArrayInputStream(byte[] buf, int offset, int length) {
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.mark = offset;
}
@Override
public synchronized int read() throws IOException {
//0xff作用是为了裁剪
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
@Override
public synchronized int read(byte[] b, int off, int len) {
//检查参数合法性
boolean argsIllegaled = checkParamArgumentLegaled(b, off, len);
if (!argsIllegaled) {
return 0;
}
//如果当前的位置大于总尺寸 则返回-1
if (pos >= count) {
return -1;
}
int avail = count - pos;
//如果要读取的长度大于可读 则把刻度的大小传递给长度
if (len > avail) {
len = avail;
}
//数组拷贝
System.arraycopy(buf, pos, b, off, len);
//位置偏移
pos += len;
//返回度过的长度
return len;
}
/**
* 跳过和丢弃
*
* @param n
* @return
*/
@Override
public synchronized long skip(long n) {
//查询当前剩下的位置个数
long k = count - pos;
//判断参数
if (n < k) {
k = n < 0 ? 0 : n;
}
pos += k;
return k;
}
@Override
public synchronized int available() {
return count - pos;
}
@Override
public boolean markSupported() {
return true;
}
/**
* 参数并没有什么卵用
*
* @param readAheadLimit
*/
@Override
public void mark(int readAheadLimit) {
mark = pos;
}
@Override
public synchronized void reset() {
pos = mark;
}
@Override
public void close() throws IOException {
}
public static void main(String[] args) throws IOException {
byte[] source = "HelloWorld".getBytes();
LHInputStream inputStream = new LHByteArrayInputStream(source);
byte[] target = new byte[512];
int length = inputStream.read(target);
System.out.println(new String(target, 0, length));
}
}
标签:nbsp pac import exce available OLE math 数据 参数
原文地址:https://www.cnblogs.com/KanHin/p/10064127.html