日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IO 流 自定义字节流的缓冲区-read 和write 的特点

發布時間:2025/3/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IO 流 自定义字节流的缓冲区-read 和write 的特点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 package com.yyq; 2 /* 3 * 字節流的緩沖區 4 * 為什么會造成這種問題呢? 5 * 小原理: 第一個字節返回的是 -1??? 為什么會是 -1呢?? 6 * 11111110000110101000 7 * 讀一個字節 ,讀取到了8個二進制位 1111-1111 -1 8 * byte 類型 的 -1 --------》int: -1 9 * 11111111 -1 讀到連續八個1 就會停下來 10 * 補位的時候 它補的是 00000000 能保證不會停下來 11 * 那么只要在前面補0 ,既可以保留原字節的數據不變,又可以避免-1的出現 12 * 11111111 11111111 11111111 11111111 13 * 00000000 00000000 00000000 11111111 14 * ------------------------------------ 15 * 00000000 00000000 00000000 11111111 進行與操作 16 * read 進行了強制轉換,將最低8位保留下來了 17 * write 方法在提升,4個字節 18 * 這種機制保證了數據的原樣性 19 */ 20 import java.io.*; 21 class MyBufferedInputStream{ 22 private InputStream in; 23 24 private byte[] buf = new byte[1024]; 25 private int pos = 0; 26 private int count = 0; 27 MyBufferedInputStream(InputStream in){ 28 this.in = in; 29 } 30 // 自定義一個read 方法 31 //一次讀一個字節,重緩沖區)(字節數組)獲取 32 public int myRead() throws IOException{ 33 // 通過in對象讀取硬盤上的數據,并存儲在buf中 34 if(count == 0){ 35 36 count = in.read(buf); 37 if(count<0){ 38 return -1; 39 } 40 pos = 0; 41 byte b = buf[pos]; 42 count--; 43 pos++; 44 return b&255; 45 } 46 else if(count>0){ 47 byte b = buf[pos]; 48 count--; 49 pos++; 50 return b&255; 51 } 52 return -1; 53 } 54 public void myClose() throws Exception{ 55 in.close(); 56 } 57 } 58 public class CopyMP3 { 59 public static void main(String[] args) throws Exception { 60 long start = System.currentTimeMillis(); 61 copy_1(); 62 long end = System.currentTimeMillis(); 63 System.out.println(end-start); 64 } 65 public static void copy_1() throws Exception{ 66 MyBufferedInputStream bufis = new MyBufferedInputStream( 67 new FileInputStream("1.jpg")); 68 BufferedOutputStream bufos = new BufferedOutputStream( 69 new FileOutputStream("2.jpg")); 70 byte[] buf = new byte[1024]; 71 int num = 0; 72 while((num = bufis.myRead())!=-1){ 73 bufos.write(num); 74 } 75 } 76 }

?

轉載于:https://www.cnblogs.com/yangyongqian/p/5153271.html

總結

以上是生活随笔為你收集整理的IO 流 自定义字节流的缓冲区-read 和write 的特点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。