Absolute Put and Get

The putters and getters you've seen so far have all been relative. That is, they put or got the data at the current position, and incremented the position accordingly. Some buffers also support absolute puts and gets. That is, they store or retrieve an element at a particular location in the buffer, irrespective of the position (though the limit and the capacity are still respected). For example, these are the absolute put( ) and get( ) methods for the ByteBuffer class. Each takes an index that is used instead of the current position:

public abstract ByteBuffer put(int index, byte b)
public abstract byte get(int index)

If the index is less than zero or greater than or equal to the buffer's limit, these methods throw an IndexOutOfBoundsException. Otherwise, their use is straightforward. For example, this code fragment creates the holey buffer shown in Figure 14-14. Notice that these methods have no effect on the position or the limit:

Figure 14-14. A byte buffer that's been filled out of order

ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put(3, (byte) 1);
buffer.put(7, (byte) 2);
buffer.put(1, (byte) 3);

The absolute methods for the other six buffer classes are similar, aside from the obvious type changes. For instance, these are the equivalent methods for DoubleBuffer:

public abstract DoubleBuffer put(int index, double x)
public abstract double get(int index)

There are no absolute bulk get and put methods.

The absolute get and put operations are optional. Buffer objects are not guaranteed to support them. If a particular buffer object does not allow absolute gets or puts, and you attempt one anyway, the method will throw an UnsupportedOperationException. However, I've never encountered this in practice, and all buffers included with the JDK do support these methods.

As an example, suppose you've stored a GIF file into a ByteBuffer named gifBuffer, and you want to find the width and height of the image. The width is always found in the seventh and eighth bytes of the file (i.e., bytes 6 and 7, since the first byte is byte 0). The height is always found in the ninth and tenth bytes of the file. Both are unsigned little-endian shorts. We can read those values like this:

byte width1 = gifBuffer.get(6);
byte width2 = gifBuffer.get(7);
byte height1 = gifBuffer.get(8);
byte height2= gifBuffer.get(9);
int width = (width2 << 8) | width1;
int height = (height2 << 8) | height1;

The current position in the buffer is irrelevant. The width and the height are always found in bytes 6 to 9.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net