11.5. Data Buffers

Now I've peeled back all but one of the layers of BufferedImage. The last layer around the actual image data is the java.awt.image.DataBuffer class. Instances of this class are containers for one or more data arrays, called banks. A DataBuffer includes methods for accessing the data contained in its banks. (I won't detail these methods here, however, as you probably won't ever have to use them.)

You may have to construct a DataBuffer someday, however, so I'll talk about each of DataBuffer's subclasses and their constructors.

First, take a look at the data type constants that have popped up throughout this chapter. They're defined right here in the DataBuffer class. These constants simply represent different data types. (The same constants are also used in Raster and SampleModel to describe transfer types.)

public static int TYPE_BYTE

public static int TYPE_USHORT

public static int TYPE_SHORT

public static int TYPE_INT

public static int TYPE_FLOAT

public static int TYPE_DOUBLE

public static int TYPE_UNDEFINED

The only types supported in the 2D API are TYPE_BYTE, TYPE_USHORT, and TYPE_INT.

The DataBuffer class has four subclasses that encapsulate byte, short, and int arrays. They are aptly named DataBufferByte, DataBufferShort, DataBuffer-UShort, and DataBufferInt. Because these classes are so similar, I will only describe the constructors for DataBufferByte. You can figure out the constructors for the other classes just by changing the types as appropriate. ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.