Interface ArrayAccess
- All Superinterfaces:
ArrayDescription
- All Known Implementing Classes:
DefaultArrayAccess
ChunkStepper
class is provided as a convenience to assist with this sort of
processing.
The accessor maintains an offset position which determines the starting
position of the next read/write. This may be modified by the
setOffset or setPosition methods. The mapping between offset values
and position vectors is determined by the pixel ordering scheme
of this ArrayAccess (as determined by its OrderedNDShape
).
Not all methods will work on a given accessor, depending on what it is capable of; if the isRandom method returns false then the offset may not be set to a position before its current position; if isReadable returns false then the read and readTile methods will fail; and if isWritable returns false then the write and writeTile methods will fail. In each of these cases the illegal accesses will result in an UnsupportedOperationException.
If the read or write methods result in an IOException this will have the side effect of closing this accessor for further access (since under these circumstances the current offset may not be known).
- Author:
- Mark Taylor (Starlink)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Shuts down this accessor for further data access.Returns a single primitive array holding all the data of this array.long
Returns the current offset into the array for read/write.long[]
Returns the coordinates at which the next read/write will occur.boolean
isMapped()
Indicates whether mapped access is available.void
Reads a number of pixels from the current offset into a specified part of a supplied primitive array.void
Reads a tile of pixels into a supplied primitive array.void
setOffset
(long off) Sets the offset into the array for the next read/write to occur.void
setPosition
(long[] pos) Sets the coordinates for the next read/write to occur.void
Writes a number of pixels starting at the current offset from a specified part of a supplied primitive array.void
Writes a tile of pixels from a supplied primitive array.Methods inherited from interface uk.ac.starlink.array.ArrayDescription
getBadHandler, getShape, getType, isRandom, isReadable, isWritable
-
Method Details
-
getOffset
long getOffset()Returns the current offset into the array for read/write.- Returns:
- the index of the next element to be read/written
-
setOffset
Sets the offset into the array for the next read/write to occur. Attempting to set the offset to a lower value than its current one will fail if random access is not available (isRandom is false).- Parameters:
off
- the position at which the next read/write will start- Throws:
IOException
- if some unexpected I/O error occursIndexOutOfBoundsException
- if off<0 or off>=npixelIllegalStateException
- if this accessor has been closedUnsupportedOperationException
- if an attempt is made to set the offset to a value lower than its current one, and random access is not available for this accessor
-
getPosition
long[] getPosition()Returns the coordinates at which the next read/write will occur.- Returns:
- an N-element array giving the coordinates of the next read/write
-
setPosition
Sets the coordinates for the next read/write to occur. Attempting to set the coordinates so that the new offset corresponds to a lower value than its current one will fail if random access is not available (isRandom returns false).- Parameters:
pos
- an N-element array giving the coordinates for the next read/write- Throws:
IOException
- if some unexpected I/O error occursIndexOutOfBoundsException
- if pos is outside the arrayIllegalStateException
- if this accessor has been closedUnsupportedOperationException
- if an attempt is made to set the offset to a value lower than its current one, and random access is not available
-
read
Reads a number of pixels from the current offset into a specified part of a supplied primitive array. The current offset will be updated accordingly (to the point after the read pixels).An IOException during the read will have the effect of closing this accessor for further access.
- Parameters:
buffer
- an array of the appropriate primitive type for this accessor into whose elementsstart..start+size
the nextsize
pixels will be readstart
- the starting offset into buffer into which the pixels should be readsize
- the number of pixels to read (also the amount by which the current offset will be incremented)- Throws:
IOException
- if there is an I/O errorUnsupportedOperationException
- if this accessor is not readable (isReadable returns false)IllegalStateException
- if this accessor has been closedIllegalArgumentException
- if buffer is not an array of primitives with type matching the type of this accessor, or if it has less than start+size elements
-
readTile
Reads a tile of pixels into a supplied primitive array. A tile is an N-dimensional hypercuboid specified by an NDShape object. The pixels are read into a given array in the order implied by the ordering scheme of this object. The specified tile must have the same dimensionality as this accessor, but need not lie wholly or partially within its bounds -- pixels outside the intersection will be given the bad value.The current offset will be updated to the point after the last pixel in the intersection between the tile and this accessor.
It is possible to read a tile when random access is not available, but only if the first pixel in the requested tile is ahead of the current offset.
An IOException during the read will have the effect of closing this accessor for further access.
- Parameters:
buffer
- an array of the appropriate primitive type for this accessor and at least as long as the number of pixels in tileShapetileShape
- an NDShape object specifying the shape of the tile to be read- Throws:
IOException
- if there is an I/O errorUnsupportedOperationException
- if this object is not readable (isReadable returns false) or if it would be necessary to read a part of the data earlier than the current offset and random access is not availableIllegalArgumentException
- if buffer is not an array of the right primitive type or has too few elementsIllegalStateException
- if this accessor has been closed
-
write
Writes a number of pixels starting at the current offset from a specified part of a supplied primitive array. The current offset will be updated accordingly (to the point after the last write).If an IOException occurs during the read, this will have the additional effect of closing this accessor for further access.
- Parameters:
buffer
- an array of the appropriate primitive type for this NDArray whose elementsstart..start+size
will be written outstart
- the starting point in buffer from which pixels will be writtensize
- the number of pixels to write (also the amount by which the current offset will be incremented)- Throws:
IOException
- if there is an I/O errorUnsupportedOperationException
- if this accessor is not writable (isWritable returns false)IllegalStateException
- if this accessor has been closedIllegalArgumentException
- if buffer is not an array of primitives with type matching the type of this accessor, or if it has less than start+size elements
-
writeTile
Writes a tile of pixels from a supplied primitive array. A tile is an N-dimensional hypercuboid specified by an NDShape object. The ordering of pixels in the array is implied by the ordering scheme of this object. The specified tile must have the same dimensionality as this accessor, but need not lie wholly or partially within its bounds -- pixels outside the intersection will simply be ignored.The current offset will be updated to the point after the last pixel in the intersection between the tile and this accessor.
It is possible to read a tile when random access is not available, but only if the first pixel in the requested tile is ahead of the current offset.
If an IOException occurs during the write, this will have the additional effect of closing this accessor for further access.
- Parameters:
buffer
- an array of the appropriate primitive type for this NDArray and at least as long as the number of pixels in tileShape (elements after this limit will be ignored)tileShape
- an NDShape object specifying the shape of the tile to be written- Throws:
IOException
- if there is an I/O errorUnsupportedOperationException
- if this accessor is not writable (isWritable returns false) or if it would be necessary to write a part of the data earlier than the current offset and random access is not availableIllegalArgumentException
- if buffer is not an array of the right primitive type or has too few elementsIllegalStateException
- if this accessor has been closed
-
isMapped
boolean isMapped()Indicates whether mapped access is available. If true, the getMapped method will return a reference to the java primitive array containing all the pixels of this NDArray. The return value must not change over the lifetime of this object.- Returns:
- true if mapped access is possible
-
getMapped
Object getMapped()Returns a single primitive array holding all the data of this array. Calling this method does not do significant work, but returns a reference to an existing mapped array. Access to the array data using this method, if available, will be more efficient than by using the read/write methods. In the case of a writable accessor, making changes to the returned primitive array will result in changes to the accessor pixel data. In the case of an accessor which is not writable, the effect of making changes to the returned array is undefined; in particular it may result in an exception.The method will fail unless isMapped returns true.
- Returns:
- a primitive array, of type implied by the Type of this object, containing all the pixels of this array, in its natural ordering
- Throws:
UnsupportedOperationException
- if mapped access is not availableIllegalStateException
- if this accessor has been closed
-
close
Shuts down this accessor for further data access. Following a call toclose
the offset will have an illegal value and calls to any read, write or position setting methods will fail with an IllegalStateException. Aclose
should always be called on an ArrayAccess when it is finished with. In the case of a readable object it enables release of associated resources beyond those taken care of by the garbage collector, and in the case of writable object it may also be required to ensure that data is actually flushed to the underlying data storage. This method may harmlessly be called on an accessor which has already been closed.- Throws:
IOException
- if there is an I/O error
-