Tải bản đầy đủ - 0 (trang)
Figure 2-13. Creating a slice buffer

Figure 2-13. Creating a slice buffer

Tải bản đầy đủ - 0trang

To create a buffer that maps to positions 12-20 (nine elements) of a preexisting array,

code like this does the trick:

char [] myBuffer = new char [100];

CharBuffer cb = CharBuffer.wrap (myBuffer);

cb.position(12).limit(21);

CharBuffer sliced = cb.slice();



A more complete discussion of view buffers can be found in Section 2.4.3.



2.4 Byte Buffers

In this section, we'll take a closer look at byte buffers. There are buffer classes for all the

primitive data types (except boolean), but byte buffers have characteristics not shared by

the others. Bytes are the fundamental data unit used by the operating system and its I/O

facilities. When moving data between the JVM and the operating system, it's necessary to

break down the other data types into their constituent bytes. As we'll see in the following

sections, the byte-oriented nature of system-level I/O can be felt throughout the design of

buffers and the services with which they interact.

For reference, here is the complete API of ByteBuffer. Some of these methods have

been discussed in previous sections and are simply type-specific versions. The new

methods will be covered in this and following sections.

package java.nio;

public abstract class ByteBuffer extends Buffer

implements Comparable

{

public static ByteBuffer allocate (int capacity)

public static ByteBuffer allocateDirect (int capacity)

public abstract boolean isDirect();

public static ByteBuffer wrap (byte[] array, int offset, int length)

public static ByteBuffer wrap (byte[] array)

public

public

public

public

public

public



abstract ByteBuffer duplicate();

abstract ByteBuffer asReadOnlyBuffer();

abstract ByteBuffer slice();

final boolean hasArray()

final byte [] array()

final int arrayOffset()



46



public

public

public

public



abstract byte get();

abstract byte get (int index);

ByteBuffer get (byte[] dst, int offset, int length)

ByteBuffer get (byte[] dst, int offset, int length)



public

public

public

public

public



abstract ByteBuffer put (byte b);

abstract ByteBuffer put (int index, byte b);

ByteBuffer put (ByteBuffer src)

ByteBuffer put (byte[] src, int offset, int length)

final ByteBuffer put (byte[] src)



public final ByteOrder order()

public final ByteBuffer order (ByteOrder bo)

public

public

public

public

public

public



abstract

abstract

abstract

abstract

abstract

abstract



CharBuffer asCharBuffer();

ShortBuffer asShortBuffer();

IntBuffer asIntBuffer();

LongBuffer asLongBuffer();

FloatBuffer asFloatBuffer();

DoubleBuffer asDoubleBuffer();



public

public

public

public



abstract

abstract

abstract

abstract



char getChar();

char getChar (int index);

ByteBuffer putChar (char value);

ByteBuffer putChar (int index, char value);



public

public

public

public



abstract

abstract

abstract

abstract



short getShort();

short getShort (int index);

ByteBuffer putShort (short value);

ByteBuffer putShort (int index, short value);



public

public

public

public



abstract

abstract

abstract

abstract



int getInt();

int getInt (int index);

ByteBuffer putInt (int value);

ByteBuffer putInt (int index, int value);



public

public

public

public



abstract

abstract

abstract

abstract



long getLong();

long getLong (int index);

ByteBuffer putLong (long value);

ByteBuffer putLong (int index, long value);



public

public

public

public



abstract

abstract

abstract

abstract



float getFloat();

float getFloat (int index);

ByteBuffer putFloat (float value);

ByteBuffer putFloat (int index, float value);



public

public

public

public



abstract

abstract

abstract

abstract



double getDouble();

double getDouble (int index);

ByteBuffer putDouble (double value);

ByteBuffer putDouble (int index, double value);



public

public

public

public

public



abstract ByteBuffer compact();

boolean equals (Object ob) {

int compareTo (Object ob) {

String toString()

int hashCode()



47



}



Bytes Are Always Eight Bits, Right?

These days, bytes are almost universally recognized as being eight bits. But this

wasn't always the case. In ages past, bytes ranged anywhere from 3 to 12 or

more bits each, with the most common being 6 to 9 bits. The eight-bit byte was

arrived at through a combination of practicality and market forces. It's practical

because eight bits are enough to represent a usable character set (English

characters anyway), eight is a power of two (which makes hardware design

simpler), eight neatly holds two hexadecimal digits, and multiples of eight

provide enough combined bits to store useful numeric values. The market force

was IBM. The IBM 360 mainframe, first introduced in the 1960s, used eight-bit

bytes. That pretty much settled the matter. For further background, consult the

man himself, Bob Bemer of IBM, at http://www.bobbemer.com/BYTE.HTM.

2.4.1 Byte Ordering

The nonbyte primitive types, except for boolean,[2] are composed of several bytes grouped

together. The data types and their sizes are summarized in Table 2-1.

[2] Booleans represent one of two values: true or false. A byte can take on 256

unique values, so a boolean cannot be unambiguously mapped to one or several bytes.

Bytes are the building blocks from which all buffers are constructed. The NIO architects

determined that implementation of boolean buffers would be problematic, and the need

for such a buffer type was debatable anyway.



Data type

Byte

Char

Short

Int

Long

Float

Double



Table 2-1. Primitive data types and sizes

Size (in bytes)

1

2

2

4

8

4

8



Each of the primitive data types is stored in memory as a contiguous sequence of bytes.

For example, the 32-bit int value 0x037FB4C7 (decimal 58,700,999) might be packed

into memory bytes as illustrated in Figure 2-14 (memory addresses increasing left to

right). Notice the word "might" in the previous sentence. Although the size of a byte has

been settled, the issue of byte order has not been universally agreed upon. The bytes

representing an integer value might just as easily be organized in memory as shown in

Figure 2-15.

Figure 2-14. Big-endian byte order



48



Figure 2-15. Little-endian byte order



The way multibyte numeric values are stored in memory is commonly referred to as

endian-ness. If the numerically most-significant byte of the number, the big end, is at the

lower address, then the system is big-endian (Figure 2-14). If the least-significant byte

comes first, it's little-endian (Figure 2-15).

Endian-ness is rarely a choice for software designers; it's usually dictated by the hardware

design. Both types of endian-ness, sometimes known as byte sex, are in wide-spread use

today. There are good arguments for both approaches. Intel processors use the

little-endian design. The Motorola CPU family, Sun Sparc, and PowerPC CPU

architectures are all big-endian.

The question of byte order even transcends CPU hardware design. When the architects of

the Internet were designing the Internet Protocol (IP) suite to interconnect all types of

computers, they recognized the problem of exchanging numeric data between systems

with differing internal byte orders. Therefore, the IPs define a notion of network byte

order,[3] which is big-endian. All multibyte numeric values used within the protocol

portions of IP packets must be converted between the local host byte order and the

common network byte order.

[3] Internet terminology refers to bytes as octets. As mentioned in the sidebar, the size of

a byte can be ambiguous. By using the term "octet," the IP specifications explicitly

mandate that bytes consist of eight bits.

In java.nio, byte order is encapsulated by the ByteOrder class:

package java.nio;

public final class ByteOrder

{

public static final ByteOrder BIG_ENDIAN

public static final ByteOrder LITTLE_ENDIAN

public static ByteOrder nativeOrder()

public String toString()

}



The ByteOrder class defines the constants that determine which byte order to use when

storing or retrieving multibyte values from a buffer. The class acts as a type-safe

49



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 2-13. Creating a slice buffer

Tải bản đầy đủ ngay(0 tr)

×