Java.nio.HeapByteBuffer Heap byte buffer source analysis _______1.1.2

Source: Internet
Author: User
Analysis of 1.ByteBuffer

http://blog.csdn.net/ya_1249463314/article/details/79271333


2. What is a heap byte buffer

The Heapbytebuffer heap byte buffer is the allocation of buffers to the heap heap in the JVM, whose implementation itself is a byte array, actually the user memory in the operating system, not kernel memory.


3. Owning Package

Package Java.nio;

4. Inheritance and implementation relationships

Class Heapbytebuffer extends Bytebuffer

5. Construction Device

/**
	 * Specifies the parameter builder,
	 * Initialize tag mark is-1, set heap buffer array space size to cap, byte array offset of 0
    /heapbytebuffer (int cap, int lim) {            
        Super ( -1, 0, Lim, Cap, new Byte[cap], 0);
    }

	/**
	 * Specifies the parameter constructor,
	 * Initialize tag mark is-1, set the starting position of read/write to OFF, read-write maximum capacity is Off+len * Set the maximum capacity of the
	 buffer to the capacity of the BUF byte array, set the heap buffer array to buf, The offset of the byte array is 0
	 *
    /Heapbytebuffer (byte[] buf, int off, int len) { 
        super ( -1, off, off + len, buf.length, buf, 0);
    }

	/**
	 * Specifies the parameter constructor,
	 * Initialize mark as Mark, set read/write start position as POS, read-write maximum capacity is Lim
	 * Set buffer maximum capacity as cap, set heap buffer array to buf, byte array offset is off
	 * *
    protected Heapbytebuffer (byte[] buf,
                                   int mark, int pos, int lim, int cap,
                                   int off)
    {
        Super (Mark, POS, Lim, Cap, buf, off);
    

6. Common Methods

/** * Creates a new heap byte buffer, the contents of this buffer are shared subsequence * Tag mark is-1, read-write start position is 0, read-write maximum capacity for buffer remaining element capacity * buffer maximum capacity for buffer remaining element capacity, byte array offset for current read-write position plus original byte
                                        Offset of array/public bytebuffer slice () {return new Heapbytebuffer (HB,
                                        -1, 0, this.remaining (),
    This.remaining (), this.position () + offset);  /** * Create a new heap byte buffer that shares the contents of this buffer * Mark mark is the current buffer, the start position is the pos of the current buffer, * Read and write maximum capacity is the current buffer of Lim, the maximum capacity of the buffer is the maximum capacity of the current buffer cap *
                                        The offset of the array is the offset of the current array/public Bytebuffer duplicate () {return new Heapbytebuffer (HB,
                                        This.markvalue (), This.position (),
                                        This.limit (), this.capacity (),
										
    Offset);
}

	/**	 * Create a read-only heap byte buffer * Mark mark as the current buffer the starting position of read-write is the current buffer's POS, * read-write maximum capacity is the current buffer of Lim, the buffer maximum capacity of the current buffer maximum capacity cap * Array offset is the current array of
                                     Shift offset/public bytebuffer Asreadonlybuffer () {return new Heapbytebufferr (HB, This.markvalue (), this.position (), t
									 
    His.limit (), this.capacity (), offset);
    //sets the offset of the read byte array to i+offset protected int IX (int i) {return i + offset;
    //Gets the next byte public byte getting () {return Hb[ix (Nextgetindex ())];
    //Get the specified subscript byte public byte gets (int i) {return Hb[ix (CheckIndex (i))]; The byte array in the current buffer is copied from position plus 1 to the input byte array in DST public bytebuffer get (byte[] DST, int offset, int length) {//check subscript
		Trans-checkbounds (offset, length, dst.length); Check for buffer overflow if (length > remaining ()) throw new BuFferunderflowexception ();
		Position+offset the byte array at the specified position in the buffer to the specified DST byte array system.arraycopy (HB, IX (Position ()), DST, offset, length);
		Set the starting position for reading and writing to position+length position (position () + length);
    Returns the current byte buffer return this;
    The//buffer is directly on the kernel buffer and returns false because it is now in the JVM heap byte buffer public boolean isdirect () {return false;
    The//buffer is read-only public boolean isreadonly () {return false;
        }//Storage byte x to byte buffer, return byte buffer public bytebuffer put (byte x) {Hb[ix (Nextputindex ())] = x;
    return this;
        ///Update bytes of the location of the specified byte buffer to x, return byte buffer public bytebuffer put (int i, byte x) {Hb[ix (CheckIndex (i))] = x;
    return this; The elements of the specified byte array src are stored in the current byte buffer public bytebuffer put (byte[] src, int offset, int length) {//Check the subscript for bounds Chec
		Kbounds (offset, length, src.length);
		Check to see if buffer overflows if (length > remaining ()) throw new Bufferoverflowexception ();
  Copies the byte into the current byte buffer by copying the SRC from the specified offset of the input byte array      System.arraycopy (SRC, offset, HB, IX (Position ()), length);
		Sets the read-write start position in the current byte buffer to the original position plus the length position (position () + length) of the new byte array;
    Returns the current byte buffer return this; The bytes of the specified byte buffer are stored to the current byte buffer public bytebuffer put (bytebuffer src) {//To determine if SRC is a heap byte buffer if (src instanceof He
			Apbytebuffer) {if (src = this) throw new IllegalArgumentException ();
			Gets the heap byte buffer heapbytebuffer sb = (heapbytebuffer) src;
            Gets the capacity int n = sb.remaining () of the remaining bytes in the byte buffer;
			if (n > Remaining ()) throw new Bufferoverflowexception (); Copies the bytes of the specified byte buffer into the current byte buffer system.arraycopy (SB.HB, Sb.ix (Sb.position ()), HB, IX (p
			Osition ()), n);
			Sets the read-write start position of the input byte buffer src sb.position (sb.position () + N);
        Sets the read and write start position of the current buffer position (position () + N); else if (Src.isdirect ()) {//Determine if SRC is a direct kernel buffer//get the byte capacity of the input byte buffer to n int n = src.reMaining ();
			if (n > Remaining ()) throw new Bufferoverflowexception ();
			Writes the bytes in the specified byte buffer src to the current byte buffer hb Src.get (HB, IX (Position ()), n);
        The start position of reading and writing in the set buffer is position position (position () + N);
        else {//Transfer the element in the specified buffer entered into another buffer super.put (SRC);
    return to this; //Compress current byte buffer public Bytebuffer compact () {//copy byte array in current buffer from Position+offset to HB system.arraycopy (HB
		, IX (Position ()), HB, IX (0), remaining ());
		Sets the starting position for reading and writing in the current buffer to be the remaining element size of the byte array position (remaining ());
		Sets the maximum capacity of read/write to the maximum capacity of the current buffer limit (capacity ());
        Set tag mark as-1 discardmark ();
    return this;
    //Get the specified subscript byte byte _get (int i) {return hb[i];
    ///Update the byte of the specified subscript to B void _put (int i, byte b) {hb[i] = b; }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.