聲明:本系列只供本人自學(xué)使用弹谁,勿噴。
前面我們已經(jīng)研究了文件流句喜、對(duì)象流预愤,但是前提呢,是需要提供文件咳胃、對(duì)象才能繼續(xù)進(jìn)行流的操作植康,那假如目前有一個(gè)字節(jié)數(shù)組{0x61, 0x62, 0x63, 0x64, 0x65, 0x66}需要進(jìn)行傳輸或者存儲(chǔ)到文件,首先得將其轉(zhuǎn)換為二進(jìn)制流展懈,這時(shí)就用到了輕量的ByteArrayInputStream和ByteArrayOutputStream销睁。假如需要操作的是字符數(shù)組{'a','b','c','d','e','f'},則要用到CharArrayReader和CharArrayWriter存崖。
一冻记、ByteArrayInputStream
- 內(nèi)部有緩沖數(shù)組
- 內(nèi)部有計(jì)數(shù)器追蹤下一個(gè)字節(jié)
- 關(guān)閉ByteArrayInputStream后仍然可以對(duì)其進(jìn)行操作。
public class ByteArrayInputStream extends InputStream {
protected byte buf[];
protected int pos;
protected int mark = 0;
protected int count;
// 傳入的字節(jié)數(shù)組作為緩沖數(shù)組
public ByteArrayInputStream(byte buf[]) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
}
public ByteArrayInputStream(byte buf[], int offset, int length)
}
- 核心方法
// 返回下一個(gè)字節(jié)的值来惧,沒有則-1
public synchronized int read()
// copy緩沖區(qū)數(shù)組buf的未讀值到byte b[]中
public synchronized int read(byte b[], int off, int len)
- 其他方法
public synchronized long skip(long n)
public synchronized int available()
public boolean markSupported()
public void mark(int readAheadLimit)
public synchronized void reset()
demo
簡(jiǎn)單操作
// byte[] byteArray="abcdef".getBytes();
byte[] byteArray={0x61, 0x62, 0x63, 0x64, 0x65, 0x66};
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
int b;
while ((b=byteArrayInputStream.read())!=-1) {
System.out.println(b);
}
實(shí)現(xiàn)將Base64圖片保存到文件
注:http://imgbase64.duoshitong.com/上傳圖片生成Base64編碼冗栗,替換代碼第一行content
String content="data:image/jpeg;base64,/9j/4AAQSkZJRg...省略很長(zhǎng)很長(zhǎng)..bP6oP/2Q==";
Pattern pattern= Pattern.compile("data:image/(.*?);base64,");
Matcher matcher=pattern.matcher(content);
String type="";
if(matcher.find()){
type=matcher.group(1);
}else{
return;
}
String base64=content.replaceAll(matcher.group(0),"");
byte[] buffer=new BASE64Decoder().decodeBuffer(base64);
ByteArrayInputStream bis=new ByteArrayInputStream(buffer);
// 以下全部代碼可以直接用 Files.copy(bis,Paths.get("D:\\test."+type))實(shí)現(xiàn)
OutputStream outputStream= Files.newOutputStream(Paths.get("D:\\test."+type));
byte[] bytes=new byte[1024];
int len=0;
while ((len=bis.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
outputStream.close();
bis.close();
二、ByteArrayOutputStream
public class ByteArrayOutputStream extends OutputStream {
protected byte buf[];
protected int count;
public ByteArrayOutputStream() {
this(32);
}
public ByteArrayOutputStream(int size){
buf = new byte[size];
}
- 核心方法
// 往buf中寫入一個(gè)字節(jié)
public synchronized void write(int b)
// copy入?yún)?byte b[]到緩沖區(qū)數(shù)組buf
public synchronized void write(byte b[], int off, int len)
// 將緩沖區(qū)數(shù)組buf寫入其他輸出流
public synchronized void writeTo(OutputStream out) throws IOException
// 返回buf
public synchronized byte toByteArray()[]
public synchronized int size()
public synchronized String toString()
demo
簡(jiǎn)單操作
byte[] byteArray={0x61, 0x62, 0x63, 0x64, 0x65, 0x66};
ByteArrayOutputStream baos=new ByteArrayOutputStream();
baos.write(byteArray);
baos.write(0x67);
baos.writeTo(Files.newOutputStream(Paths.get("D:\\test.txt")));
CharArrayReader和CharArrayWriter源碼過于相似供搀,只是數(shù)組是char型隅居,自行查看。