BufferedInputStream 介紹
BufferedInputStream 是緩沖輸入流。它繼承于FilterInputStream。
BufferedInputStream 的作用是為另一個(gè)輸入流添加一些功能,例如,提供“緩沖功能”以及支持“mark()標(biāo)記”和“reset()重置方法”。
BufferedInputStream 本質(zhì)上是通過一個(gè)內(nèi)部緩沖區(qū)數(shù)組實(shí)現(xiàn)的。例如,在新建某輸入流對(duì)應(yīng)的BufferedInputStream后,當(dāng)我們通過read()讀取輸入流的數(shù)據(jù)時(shí),BufferedInputStream會(huì)將該輸入流的數(shù)據(jù)分批的填入到緩沖區(qū)中。每當(dāng)緩沖區(qū)中的數(shù)據(jù)被讀完之后,輸入流會(huì)再次填充數(shù)據(jù)緩沖區(qū);如此反復(fù),直到我們讀完輸入流數(shù)據(jù)位置。
BufferedInputStream 函數(shù)列表
1
2
3
4
5
6
7
8
9
10
11
|
BufferedInputStream(InputStream in) BufferedInputStream(InputStream in, int size) synchronized int available() void close() synchronized void mark( int readlimit) boolean markSupported() synchronized int read() synchronized int read( byte [] buffer, int offset, int byteCount) synchronized void reset() synchronized long skip( long byteCount) |
BufferedInputStream 源碼分析(基于jdk1.7.40)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
package java.io; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; public class BufferedInputStream extends FilterInputStream { // 默認(rèn)的緩沖大小是8192字節(jié) // BufferedInputStream 會(huì)根據(jù)“緩沖區(qū)大小”來逐次的填充緩沖區(qū); // 即,BufferedInputStream填充緩沖區(qū),用戶讀取緩沖區(qū),讀完之后,BufferedInputStream會(huì)再次填充緩沖區(qū)。如此循環(huán),直到讀完數(shù)據(jù)... private static int defaultBufferSize = 8192 ; // 緩沖數(shù)組 protected volatile byte buf[]; // 緩存數(shù)組的原子更新器。 // 該成員變量與buf數(shù)組的volatile關(guān)鍵字共同組成了buf數(shù)組的原子更新功能實(shí)現(xiàn), // 即,在多線程中操作BufferedInputStream對(duì)象時(shí),buf和bufUpdater都具有原子性(不同的線程訪問到的數(shù)據(jù)都是相同的) private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte []> bufUpdater = AtomicReferenceFieldUpdater.newUpdater (BufferedInputStream. class , byte []. class , "buf" ); // 當(dāng)前緩沖區(qū)的有效字節(jié)數(shù)。 // 注意,這里是指緩沖區(qū)的有效字節(jié)數(shù),而不是輸入流中的有效字節(jié)數(shù)。 protected int count; // 當(dāng)前緩沖區(qū)的位置索引 // 注意,這里是指緩沖區(qū)的位置索引,而不是輸入流中的位置索引。 protected int pos; // 當(dāng)前緩沖區(qū)的標(biāo)記位置 // markpos和reset()配合使用才有意義。操作步驟: // (01) 通過mark() 函數(shù),保存pos的值到markpos中。 // (02) 通過reset() 函數(shù),會(huì)將pos的值重置為markpos。接著通過read()讀取數(shù)據(jù)時(shí),就會(huì)從mark()保存的位置開始讀取。 protected int markpos = - 1 ; // marklimit是標(biāo)記的最大值。 // 關(guān)于marklimit的原理,我們?cè)诤竺娴膄ill()函數(shù)分析中會(huì)詳細(xì)說明。這對(duì)理解BufferedInputStream相當(dāng)重要。 protected int marklimit; // 獲取輸入流 private InputStream getInIfOpen() throws IOException { InputStream input = in; if (input == null ) throw new IOException( "Stream closed" ); return input; } // 獲取緩沖 private byte [] getBufIfOpen() throws IOException { byte [] buffer = buf; if (buffer == null ) throw new IOException( "Stream closed" ); return buffer; } // 構(gòu)造函數(shù):新建一個(gè)緩沖區(qū)大小為8192的BufferedInputStream public BufferedInputStream(InputStream in) { this (in, defaultBufferSize); } // 構(gòu)造函數(shù):新建指定緩沖區(qū)大小的BufferedInputStream public BufferedInputStream(InputStream in, int size) { super (in); if (size <= 0 ) { throw new IllegalArgumentException( "Buffer size <= 0" ); } buf = new byte [size]; } // 從“輸入流”中讀取數(shù)據(jù),并填充到緩沖區(qū)中。 // 后面會(huì)對(duì)該函數(shù)進(jìn)行詳細(xì)說明! private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos < 0 ) pos = 0 ; /* no mark: throw away the buffer */ else if (pos >= buffer.length) /* no room left in buffer */ if (markpos > 0) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0, sz); pos = sz; markpos = 0; } else if (buffer.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } else { /* grow buffer */ int nsz = pos * 2 ; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte [nsz]; System.arraycopy(buffer, 0 , nbuf, 0 , pos); if (!bufUpdater.compareAndSet( this , buffer, nbuf)) { throw new IOException( "Stream closed" ); } buffer = nbuf; } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } // 讀取下一個(gè)字節(jié) public synchronized int read() throws IOException { // 若已經(jīng)讀完緩沖區(qū)中的數(shù)據(jù),則調(diào)用fill()從輸入流讀取下一部分?jǐn)?shù)據(jù)來填充緩沖區(qū) if (pos >= count) { fill(); if (pos >= count) return - 1 ; } // 從緩沖區(qū)中讀取指定的字節(jié) return getBufIfOpen()[pos++] & 0xff ; } // 將緩沖區(qū)中的數(shù)據(jù)寫入到字節(jié)數(shù)組b中。off是字節(jié)數(shù)組b的起始位置,len是寫入長(zhǎng)度 private int read1( byte [] b, int off, int len) throws IOException { int avail = count - pos; if (avail <= 0 ) { // 加速機(jī)制。 // 如果讀取的長(zhǎng)度大于緩沖區(qū)的長(zhǎng)度 并且沒有markpos, // 則直接從原始輸入流中進(jìn)行讀取,從而避免無謂的COPY(從原始輸入流至緩沖區(qū),讀取緩沖區(qū)全部數(shù)據(jù),清空緩沖區(qū), // 重新填入原始輸入流數(shù)據(jù)) if (len >= getBufIfOpen().length && markpos < 0 ) { return getInIfOpen().read(b, off, len); } // 若已經(jīng)讀完緩沖區(qū)中的數(shù)據(jù),則調(diào)用fill()從輸入流讀取下一部分?jǐn)?shù)據(jù)來填充緩沖區(qū) fill(); avail = count - pos; if (avail <= 0 ) return - 1 ; } int cnt = (avail < len) ? avail : len; System.arraycopy(getBufIfOpen(), pos, b, off, cnt); pos += cnt; return cnt; } // 將緩沖區(qū)中的數(shù)據(jù)寫入到字節(jié)數(shù)組b中。off是字節(jié)數(shù)組b的起始位置,len是寫入長(zhǎng)度 public synchronized int read( byte b[], int off, int len) throws IOException { getBufIfOpen(); // Check for closed stream if ((off | len | (off + len) | (b.length - (off + len))) < 0 ) { throw new IndexOutOfBoundsException(); } else if (len == 0 ) { return 0 ; } // 讀取到指定長(zhǎng)度的數(shù)據(jù)才返回 int n = 0 ; for (;;) { int nread = read1(b, off + n, len - n); if (nread <= 0 ) return (n == 0 ) ? nread : n; n += nread; if (n >= len) return n; // if not closed but no bytes available, return InputStream input = in; if (input != null && input.available() <= 0 ) return n; } } // 忽略n個(gè)字節(jié) public synchronized long skip( long n) throws IOException { getBufIfOpen(); // Check for closed stream if (n <= 0 ) { return 0 ; } long avail = count - pos; if (avail <= 0 ) { // If no mark position set then don't keep in buffer if (markpos < 0 ) return getInIfOpen().skip(n); // Fill in buffer to save bytes for reset fill(); avail = count - pos; if (avail <= 0 ) return 0 ; } long skipped = (avail < n) ? avail : n; pos += skipped; return skipped; } // 下一個(gè)字節(jié)是否存可讀 public synchronized int available() throws IOException { int n = count - pos; int avail = getInIfOpen().available(); return n > (Integer.MAX_VALUE - avail) ? Integer.MAX_VALUE : n + avail; } // 標(biāo)記“緩沖區(qū)”中當(dāng)前位置。 // readlimit是marklimit,關(guān)于marklimit的作用,參考后面的說明。 public synchronized void mark( int readlimit) { marklimit = readlimit; markpos = pos; } // 將“緩沖區(qū)”中當(dāng)前位置重置到mark()所標(biāo)記的位置 public synchronized void reset() throws IOException { getBufIfOpen(); // Cause exception if closed if (markpos < 0 ) throw new IOException( "Resetting to invalid mark" ); pos = markpos; } public boolean markSupported() { return true ; } // 關(guān)閉輸入流 public void close() throws IOException { byte [] buffer; while ( (buffer = buf) != null ) { if (bufUpdater.compareAndSet( this , buffer, null )) { InputStream input = in; in = null ; if (input != null ) input.close(); return ; } // Else retry in case a new buf was CASed in fill() } } } |
說明:
要想讀懂BufferedInputStream的源碼,就要先理解它的思想。BufferedInputStream的作用是為其它輸入流提供緩沖功能。創(chuàng)建BufferedInputStream時(shí),我們會(huì)通過它的構(gòu)造函數(shù)指定某個(gè)輸入流為參數(shù)。BufferedInputStream會(huì)將該輸入流數(shù)據(jù)分批讀取,每次讀取一部分到緩沖中;操作完緩沖中的這部分?jǐn)?shù)據(jù)之后,再?gòu)妮斎肓髦凶x取下一部分的數(shù)據(jù)。
為什么需要緩沖呢?原因很簡(jiǎn)單,效率問題!緩沖中的數(shù)據(jù)實(shí)際上是保存在內(nèi)存中,而原始數(shù)據(jù)可能是保存在硬盤或NandFlash等存儲(chǔ)介質(zhì)中;而我們知道,從內(nèi)存中讀取數(shù)據(jù)的速度比從硬盤讀取數(shù)據(jù)的速度至少快10倍以上。
那干嘛不干脆一次性將全部數(shù)據(jù)都讀取到緩沖中呢?第一,讀取全部的數(shù)據(jù)所需要的時(shí)間可能會(huì)很長(zhǎng)。第二,內(nèi)存價(jià)格很貴,容量不像硬盤那么大。
下面,我就BufferedInputStream中最重要的函數(shù)fill()進(jìn)行說明。其它的函數(shù)很容易理解,我就不詳細(xì)介紹了,大家可以參考源碼中的注釋進(jìn)行理解。
fill() 源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos < 0 ) pos = 0 ; else if (pos >= buffer.length) { if (markpos > 0 ) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0, sz); pos = sz; markpos = 0; } else if (buffer.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } else { /* grow buffer */ int nsz = pos * 2 ; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte [nsz]; System.arraycopy(buffer, 0 , nbuf, 0 , pos); if (!bufUpdater.compareAndSet( this , buffer, nbuf)) { // Can't replace buf if there was an async close. // Note: This would need to be changed if fill() // is ever made accessible to multiple threads. // But for now, the only way CAS can fail is via close. // assert buf == null; throw new IOException( "Stream closed" ); } buffer = nbuf; } } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
根據(jù)fill()中的if...else...,下面我們將fill分為5種情況進(jìn)行說明。
情況1:讀取完buffer中的數(shù)據(jù),并且buffer沒有被標(biāo)記
執(zhí)行流程如下,
(01) read() 函數(shù)中調(diào)用 fill()
(02) fill() 中的 if (markpos < 0) ...
為了方便分析,我們將這種情況下fill()執(zhí)行的操作等價(jià)于以下代碼:
1
2
3
4
5
6
7
8
9
10
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos < 0 ) pos = 0 ; count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
說明:
這種情況發(fā)生的情況是 — — 輸入流中有很長(zhǎng)的數(shù)據(jù),我們每次從中讀取一部分?jǐn)?shù)據(jù)到buffer中進(jìn)行操作。每次當(dāng)我們讀取完buffer中的數(shù)據(jù)之后,并且此時(shí)輸入流沒有被標(biāo)記;那么,就接著從輸入流中讀取下一部分的數(shù)據(jù)到buffer中。
其中,判斷是否讀完buffer中的數(shù)據(jù),是通過 if (pos >= count) 來判斷的;
判斷輸入流有沒有被標(biāo)記,是通過 if (markpos < 0) 來判斷的。
理解這個(gè)思想之后,我們?cè)賹?duì)這種情況下的fill()的代碼進(jìn)行分析,就特別容易理解了。
(01) if (markpos < 0) 它的作用是判斷“輸入流是否被標(biāo)記”。若被標(biāo)記,則markpos大于/等于0;否則markpos等于-1。
(02) 在這種情況下:通過getInIfOpen()獲取輸入流,然后接著從輸入流中讀取buffer.length個(gè)字節(jié)到buffer中。
(03) count = n + pos; 這是根據(jù)從輸入流中讀取的實(shí)際數(shù)據(jù)的多少,來更新buffer中數(shù)據(jù)的實(shí)際大小。
情況2:讀取完buffer中的數(shù)據(jù),buffer的標(biāo)記位置>0,并且buffer中沒有多余的空間
執(zhí)行流程如下,
(01) read() 函數(shù)中調(diào)用 fill()
(02) fill() 中的 else if (pos >= buffer.length) ...
(03) fill() 中的 if (markpos > 0) ...
為了方便分析,我們將這種情況下fill()執(zhí)行的操作等價(jià)于以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos >= 0 && pos >= buffer.length) { if (markpos > 0 ) { int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0 , sz); pos = sz; markpos = 0 ; } } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
說明:
這種情況發(fā)生的情況是 — — 輸入流中有很長(zhǎng)的數(shù)據(jù),我們每次從中讀取一部分?jǐn)?shù)據(jù)到buffer中進(jìn)行操作。當(dāng)我們讀取完buffer中的數(shù)據(jù)之后,并且此時(shí)輸入流存在標(biāo)記時(shí);那么,就發(fā)生情況2。此時(shí),我們要保留“被標(biāo)記位置”到“buffer末尾”的數(shù)據(jù),然后再?gòu)妮斎肓髦凶x取下一部分的數(shù)據(jù)到buffer中。
其中,判斷是否讀完buffer中的數(shù)據(jù),是通過 if (pos >= count) 來判斷的;
判斷輸入流有沒有被標(biāo)記,是通過 if (markpos < 0) 來判斷的。
判斷buffer中沒有多余的空間,是通過 if (pos >= buffer.length) 來判斷的。
理解這個(gè)思想之后,我們?cè)賹?duì)這種情況下的fill()代碼進(jìn)行分析,就特別容易理解了。
(01) int sz = pos - markpos; 作用是“獲取‘被標(biāo)記位置'到‘buffer末尾'”的數(shù)據(jù)長(zhǎng)度。
(02) System.arraycopy(buffer, markpos, buffer, 0, sz); 作用是“將buffer中從markpos開始的數(shù)據(jù)”拷貝到buffer中(從位置0開始填充,填充長(zhǎng)度是sz)。接著,將sz賦值給pos,即pos就是“被標(biāo)記位置”到“buffer末尾”的數(shù)據(jù)長(zhǎng)度。
(03) int n = getInIfOpen().read(buffer, pos, buffer.length - pos); 從輸入流中讀取出“buffer.length - pos”的數(shù)據(jù),然后填充到buffer中。
(04) 通過第(02)和(03)步組合起來的buffer,就是包含了“原始buffer被標(biāo)記位置到buffer末尾”的數(shù)據(jù),也包含了“從輸入流中新讀取的數(shù)據(jù)”。
注意:執(zhí)行過情況2之后,markpos的值由“大于0”變成了“等于0”!
情況3:讀取完buffer中的數(shù)據(jù),buffer被標(biāo)記位置=0,buffer中沒有多余的空間,并且buffer.length>=marklimit
執(zhí)行流程如下,
(01) read() 函數(shù)中調(diào)用 fill()
(02) fill() 中的 else if (pos >= buffer.length) ...
(03) fill() 中的 else if (buffer.length >= marklimit) ...
為了方便分析,我們將這種情況下fill()執(zhí)行的操作等價(jià)于以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos >= 0 && pos >= buffer.length) { if ( (markpos <= 0 ) && (buffer.length >= marklimit) ) { markpos = - 1 ; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
說明:這種情況的處理非常簡(jiǎn)單。首先,就是“取消標(biāo)記”,即 markpos = -1;然后,設(shè)置初始化位置為0,即pos=0;最后,再?gòu)妮斎肓髦凶x取下一部分?jǐn)?shù)據(jù)到buffer中。
情況4:讀取完buffer中的數(shù)據(jù),buffer被標(biāo)記位置=0,buffer中沒有多余的空間,并且buffer.length<marklimit
執(zhí)行流程如下,
(01) read() 函數(shù)中調(diào)用 fill()
(02) fill() 中的 else if (pos >= buffer.length) ...
(03) fill() 中的 else { int nsz = pos * 2; ... }
為了方便分析,我們將這種情況下fill()執(zhí)行的操作等價(jià)于以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); if (markpos >= 0 && pos >= buffer.length) { if ( (markpos <= 0 ) && (buffer.length < marklimit) ) { int nsz = pos * 2 ; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte [nsz]; System.arraycopy(buffer, 0 , nbuf, 0 , pos); if (!bufUpdater.compareAndSet( this , buffer, nbuf)) { throw new IOException( "Stream closed" ); } buffer = nbuf; } } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
說明:
這種情況的處理非常簡(jiǎn)單。
(01) 新建一個(gè)字節(jié)數(shù)組nbuf。nbuf的大小是“pos*2”和“marklimit”中較小的那個(gè)數(shù)。
1
2
3
4
|
int nsz = pos * 2 ; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte [nsz]; |
(02) 接著,將buffer中的數(shù)據(jù)拷貝到新數(shù)組nbuf中。通過System.arraycopy(buffer, 0, nbuf, 0, pos)
(03) 最后,從輸入流讀取部分新數(shù)據(jù)到buffer中。通過getInIfOpen().read(buffer, pos, buffer.length - pos);
注意:在這里,我們思考一個(gè)問題,“為什么需要marklimit,它的存在到底有什么意義?”我們結(jié)合“情況2”、“情況3”、“情況4”的情況來分析。
假設(shè),marklimit是無限大的,而且我們?cè)O(shè)置了markpos。當(dāng)我們從輸入流中每讀完一部分?jǐn)?shù)據(jù)并讀取下一部分?jǐn)?shù)據(jù)時(shí),都需要保存markpos所標(biāo)記的數(shù)據(jù);這就意味著,我們需要不斷執(zhí)行情況4中的操作,要將buffer的容量擴(kuò)大……隨著讀取次數(shù)的增多,buffer會(huì)越來越大;這會(huì)導(dǎo)致我們占據(jù)的內(nèi)存越來越大。所以,我們需要給出一個(gè)marklimit;當(dāng)buffer>=marklimit時(shí),就不再保存markpos的值了。
情況5:除了上面4種情況之外的情況
執(zhí)行流程如下,
(01) read() 函數(shù)中調(diào)用 fill()
(02) fill() 中的 count = pos...
為了方便分析,我們將這種情況下fill()執(zhí)行的操作等價(jià)于以下代碼:
1
2
3
4
5
6
7
8
|
private void fill() throws IOException { byte [] buffer = getBufIfOpen(); count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0 ) count = n + pos; } |
說明:這種情況的處理非常簡(jiǎn)單。直接從輸入流讀取部分新數(shù)據(jù)到buffer中。
示例代碼
關(guān)于BufferedInputStream中API的詳細(xì)用法,參考示例代碼(BufferedInputStreamTest.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.lang.SecurityException; /** * BufferedInputStream 測(cè)試程序 * * @author skywang */ public class BufferedInputStreamTest { private static final int LEN = 5 ; public static void main(String[] args) { testBufferedInputStream() ; } /** * BufferedInputStream的API測(cè)試函數(shù) */ private static void testBufferedInputStream() { // 創(chuàng)建BufferedInputStream字節(jié)流,內(nèi)容是ArrayLetters數(shù)組 try { File file = new File( "bufferedinputstream.txt" ); InputStream in = new BufferedInputStream( new FileInputStream(file), 512 ); // 從字節(jié)流中讀取5個(gè)字節(jié)。“abcde”,a對(duì)應(yīng)0x61,b對(duì)應(yīng)0x62,依次類推... for ( int i= 0 ; i<LEN; i++) { // 若能繼續(xù)讀取下一個(gè)字節(jié),則讀取下一個(gè)字節(jié) if (in.available() >= 0 ) { // 讀取“字節(jié)流的下一個(gè)字節(jié)” int tmp = in.read(); System.out.printf( "%d : 0x%s\n" , i, Integer.toHexString(tmp)); } } // 若“該字節(jié)流”不支持標(biāo)記功能,則直接退出 if (!in.markSupported()) { System.out.println( "make not supported!" ); return ; } // 標(biāo)記“當(dāng)前索引位置”,即標(biāo)記第6個(gè)位置的元素--“f” // 1024對(duì)應(yīng)marklimit in.mark( 1024 ); // 跳過22個(gè)字節(jié)。 in.skip( 22 ); // 讀取5個(gè)字節(jié) byte [] buf = new byte [LEN]; in.read(buf, 0 , LEN); // 將buf轉(zhuǎn)換為String字符串。 String str1 = new String(buf); System.out.printf( "str1=%s\n" , str1); // 重置“輸入流的索引”為mark()所標(biāo)記的位置,即重置到“f”處。 in.reset(); // 從“重置后的字節(jié)流”中讀取5個(gè)字節(jié)到buf中。即讀取“fghij” in.read(buf, 0 , LEN); // 將buf轉(zhuǎn)換為String字符串。 String str2 = new String(buf); System.out.printf( "str2=%s\n" , str2); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
程序中讀取的bufferedinputstream.txt的內(nèi)容如下:
abcdefghijklmnopqrstuvwxyz
0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ
運(yùn)行結(jié)果:
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=01234
str2=fghij
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。