70 likes | 186 Views
Slide1: BUFFER. 1.2 Buffer Atribute : package java.nio; public abstract class Buffer { private int mark = -1; private int position = 0; private int limit; private int capacity; }
E N D
Slide1: BUFFER 1.2 Buffer Atribute: package java.nio; public abstract class Buffer { private int mark = -1; private int position = 0; private int limit; private int capacity; } (1.2: xuấthiệnsaucáihìnhquanhệcáclớptrong buffer, cóthểchocáihìnhdướichìmxuốngvàlàmnổicáinàylên :D) • 1.3 Buffer API:- Tạo buffer • Đọc/ghidữliệutrên buffer • Sửdụng view buffer • Directed buffer
Slide2a: Buffer Atribute • (2.1: xuâthiệnđầutiên) • Capacity • Limit • Position • Mask • (xuấhiệntuầntựtừngAtribute => Hiệuứngphóng to lênrùithunhỏlại => nổibật) (2.2a: xuấthiệnsau 2.1, cóthểcho 2.1 chìmxuốngcáinàycómàunổihơn :D) Vídụ 1:TạoByteBuffer 10 phầntử ByteBuffermyByteBuffer = ByteBuffer.allocate(10); (2.2b: làcáihình buffer có 10 phầntử) (2.3a:) Vídụ 2: Ghi “Hello” lênByteBuffer myBuffer.clear(); myByteBuffer.put((byte)'H').put((byte)'e').put((byte)'l').put((byte)'l').put((byte)'o'); (2.3b: HìnhcóghichữHelllo) (2.4a) Vídụ 3: Đọcdữliệutừ buffer myByteBuffer.flip(); while(buffer.hasRemaining( ) ) {myByteArray [i] = myByteBuffer.get( ); } (2.4b: làhình buffer có limit = 6)
Slide2b: Buffer Atribute public abstract class Buffer { public final int capacity( ) public final int position( ) public final int limit( ) public final Buffer position (intnewPosition) public final Buffer limit (intnewLimit) public final Buffer mark( ) public final Buffer reset( ) public final Buffer clear( ) public final Buffer flip( ) public final Buffer rewind( ) public final int remaining( ) public final booleanhasRemaining( ) public abstract booleanisReadOnly( );}
Slide 3: Buffer API • Tạo Buffer • Allocating + Wrapping • Danhsáchcác API tạoCharBuffer: public abstract class CharBuffer extends Buffer implements CharSequence, Comparable { // This is a partial API listing public static CharBuffer allocate (int capacity)public static CharBuffer wrap (char [] array) public static CharBuffer wrap (char [] array, int offset, int length) public static CharBuffer wrap (CharSequencecsq) public static CharBuffer wrap (CharSequencecsq, int start, int end) public final booleanhasArray( ) public final char [] array( ) public final intarrayOffset( ) } (3.2 xuấthiệnsaucáikhaibáo ở dưới) Vídụ: Wrapping CharBuffer: Char [] myCharArray = new char[100];CharBuffermyCharBuffer = CharBuffer.wrap(myCharArray); Vídụ: Wrapping CharBuffertừCharSequence: CharBuffermyCharBuffer = CharBuffer.wrap(“Hello my friends!”);
Slide4: Buffer API • (4.1)ĐọcGhiDữLiệuTrên Buffer • Đọc + Ghi = [Flip] + Draining + [Clear] + Filling • Danhsáchcác API đọcghitrênByteBuffer • public abstract class ByteBuffer extends Buffer implements Comparable { // This is a partial API listing public abstract byte get( ); public abstract byte get (int index); public abstract ByteBuffer put (byte b); public abstract ByteBuffer put (int index, byte b); • public final Buffer clear( ) public final Buffer flip( ) public final Buffer rewind( ) public final int remaining( ) public final booleanhasRemaining( ) } (4.2)Bulk Move public abstract class CharBuffer extends Buffer implements CharSequence, Comparable { // This is a partial API listing public CharBuffer get (char [] dst) public CharBuffer get (char [] dst, int offset, int length) public final CharBuffer put (char[] src) public CharBuffer put (char [] src, int offset, int length) public CharBuffer put (CharBuffersrc) public final CharBuffer put (String src) public CharBuffer put (String src, int start, int end) } - Vídụ: FastCopyFile.java [IBM Tutorial] .
Slide5: Buffer API • View Buffer/Duplicating public abstract class CharBuffer extends Buffer implements CharSequence, Comparable { // This is a partial API listing public abstract CharBuffer duplicate( ); public abstract CharBufferasReadOnlyBuffer(); public abstract CharBuffer slice( ); } (5.2a) Vídụ: Slicing buffer: CharBufferbuffer = CharBuffer.allocate (8); buffer.position (3).limit (5); CharBuffersliceBuffer = buffer.slice( ); (5.2b: hình minh họa) (5.3) View byte buffer public abstract class ByteBuffer extends Buffer implements Comparable { // This is a partial API listing public abstract CharBufferasCharBuffer( ); public abstract ShortBufferasShortBuffer( ); public abstract IntBufferasIntBuffer( ); public abstract LongBufferasLongBuffer( ); public abstract FloatBufferasFloatBuffer( ); public abstract DoubleBufferasDoubleBuffer( ); }
Slide5: Buffer API • Directed buffer • Nhanhhơnnhưngtốnnhiềubộnhớhơn • Cơchếtruyxuấtđếncác non-direct buffer thôngthường: 1. Tạomộtđốitượng direct ByteBufferlàmvùngnhớtạm.2. Sao chépnội dung từ non-direct buffer lênvùngnhớtạmvừatạo.3. Thựchiệncácthaotácnhậpxuấtcấpthấptrênvùngnhớtạm. • API: public abstract class ByteBuffer extends Buffer implements Comparable { // This is a partial API listing public static ByteBufferallocateDirect (int capacity) public abstract booleanisDirect( ); }