forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharBufferFlip.java
More file actions
32 lines (28 loc) · 1.11 KB
/
Copy pathCharBufferFlip.java
File metadata and controls
32 lines (28 loc) · 1.11 KB
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
import java.nio.CharBuffer;
public class CharBufferFlip{
public static void main(String[] args){
String[] poem = {"Roses are red, ",
"violets are blue. ",
"Sugar is sweet, ",
"and so are you. "};
CharBuffer buffer = CharBuffer.allocate(50);
for(int i=0; i<poem.length; i++){
// Fill the buffer.
for(int j=0; j<poem[i].length(); j++){
buffer.put(poem[i].charAt(j));
}
// Flip the buffer before reading the contents.
// flip() method just a convenient way to accomplish the task, you can invoke limit() and position() or discardMark() manually.
buffer.flip(); // set limit to be the current position, then set position to be 0, discarded any defined mark.
// Drain the buffer.
while(buffer.hasRemaining()){ // evaluate "position < limit"
System.out.print(buffer.get());
}
// Terminate the current line.
System.out.println();
// set position to be 0, limit to be capacity, discard any defined mark.
// Prepare buffer for asequence channel reading or invoking put() method. Otherwise, BufferOverflowException will be thrown.
buffer.clear();
}
}
}