下面列出了java.awt.image.DataBufferShort#javax.imageio.IIOException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
void shouldLogIOException()
{
File path = new File("route_66");
filesystemScreenshotDebugger.setDebugScreenshotsLocation(Optional.of(path));
filesystemScreenshotDebugger.debug(FilesystemScreenshotDebuggerTests.class, "",
new BufferedImage(10, 10, 5));
List<LoggingEvent> loggingEvents = testLogger.getLoggingEvents();
LoggingEvent loggingEvent = loggingEvents.get(0);
String message = loggingEvent.getMessage();
assertThat(loggingEvents, Matchers.hasSize(1));
assertEquals(Level.DEBUG, loggingEvent.getLevel());
assertEquals("Unable to save debug screenshot to {}", message);
assertThat(loggingEvent.getArguments().get(0).toString(), stringContainsInOrder(List.of(path.toString(),
"FilesystemScreenshotDebuggerTests_.png")));
assertThat(loggingEvent.getThrowable().get(), is(instanceOf(IIOException.class)));
}
/**
* Fills the data array from the stream, starting with
* the buffer and then reading directly from the stream
* if necessary. The buffer is left in an appropriate
* state. If the end of the stream is encountered, an
* <code>IIOException</code> is thrown with the
* message "Image Format Error".
*/
void readData(byte [] data) throws IOException {
int count = data.length;
// First see what's left in the buffer.
if (bufAvail >= count) { // It's enough
System.arraycopy(buf, bufPtr, data, 0, count);
bufAvail -= count;
bufPtr += count;
return;
}
int offset = 0;
if (bufAvail > 0) { // Some there, but not enough
System.arraycopy(buf, bufPtr, data, 0, bufAvail);
offset = bufAvail;
count -= bufAvail;
bufAvail = 0;
bufPtr = 0;
}
// Now read the rest directly from the stream
if (iis.read(data, offset, count) != count) {
throw new IIOException ("Image format Error");
}
}
/**
* Skips <code>count</code> bytes, leaving the buffer
* in an appropriate state. If the end of the stream is
* encountered, an <code>IIOException</code> is thrown with the
* message "Image Format Error".
*/
void skipData(int count) throws IOException {
// First see what's left in the buffer.
if (bufAvail >= count) { // It's enough
bufAvail -= count;
bufPtr += count;
return;
}
if (bufAvail > 0) { // Some there, but not enough
count -= bufAvail;
bufAvail = 0;
bufPtr = 0;
}
// Now read the rest directly from the stream
if (iis.skipBytes(count) != count) {
throw new IIOException ("Image format Error");
}
}
public int getNumImages(boolean allowSearch) throws IIOException {
if (stream == null) {
throw new IllegalStateException("Input not set!");
}
if (seekForwardOnly && allowSearch) {
throw new IllegalStateException
("seekForwardOnly and allowSearch can't both be true!");
}
if (numImages > 0) {
return numImages;
}
if (allowSearch) {
this.numImages = locateImage(Integer.MAX_VALUE) + 1;
}
return numImages;
}
private void writeApplicationExtension(GIFWritableImageMetadata im)
throws IOException {
if (im.applicationIDs != null) {
Iterator iterIDs = im.applicationIDs.iterator();
Iterator iterCodes = im.authenticationCodes.iterator();
Iterator iterData = im.applicationData.iterator();
while (iterIDs.hasNext()) {
try {
stream.write(0x21);
stream.write(0xff);
stream.write(11);
stream.write((byte[])iterIDs.next(), 0, 8);
stream.write((byte[])iterCodes.next(), 0, 3);
writeBlocks((byte[])iterData.next());
stream.write(0x00);
} catch (IOException e) {
throw new IIOException("I/O error writing Application Extension!", e);
}
}
}
}
private void writePlainTextExtension(GIFWritableImageMetadata im)
throws IOException {
if (im.hasPlainTextExtension) {
try {
stream.write(0x21);
stream.write(0x1);
stream.write(12);
stream.writeShort(im.textGridLeft);
stream.writeShort(im.textGridTop);
stream.writeShort(im.textGridWidth);
stream.writeShort(im.textGridHeight);
stream.write(im.characterCellWidth);
stream.write(im.characterCellHeight);
stream.write(im.textForegroundColor);
stream.write(im.textBackgroundColor);
writeBlocks(im.text);
stream.write(0x00);
} catch (IOException e) {
throw new IIOException("I/O error writing Plain Text Extension!", e);
}
}
}
private void writeApplicationExtension(GIFWritableImageMetadata im)
throws IOException {
if (im.applicationIDs != null) {
Iterator iterIDs = im.applicationIDs.iterator();
Iterator iterCodes = im.authenticationCodes.iterator();
Iterator iterData = im.applicationData.iterator();
while (iterIDs.hasNext()) {
try {
stream.write(0x21);
stream.write(0xff);
stream.write(11);
stream.write((byte[])iterIDs.next(), 0, 8);
stream.write((byte[])iterCodes.next(), 0, 3);
writeBlocks((byte[])iterData.next());
stream.write(0x00);
} catch (IOException e) {
throw new IIOException("I/O error writing Application Extension!", e);
}
}
}
}
/**
* Resets the current stream byte and bit positions from the stack
* of marked positions.
*
* <p> An <code>IOException</code> will be thrown if the previous
* marked position lies in the discarded portion of the stream.
*
* @exception IOException if an I/O error occurs.
*/
public void reset() throws IOException {
if (markByteStack.empty()) {
return;
}
long pos = ((Long)markByteStack.pop()).longValue();
if (pos < flushedPos) {
throw new IIOException
("Previous marked position has been discarded!");
}
seek(pos);
int offset = ((Integer)markBitStack.pop()).intValue();
setBitOffset(offset);
}
private void writeApplicationExtension(GIFWritableImageMetadata im)
throws IOException {
if (im.applicationIDs != null) {
Iterator iterIDs = im.applicationIDs.iterator();
Iterator iterCodes = im.authenticationCodes.iterator();
Iterator iterData = im.applicationData.iterator();
while (iterIDs.hasNext()) {
try {
stream.write(0x21);
stream.write(0xff);
stream.write(11);
stream.write((byte[])iterIDs.next(), 0, 8);
stream.write((byte[])iterCodes.next(), 0, 3);
writeBlocks((byte[])iterData.next());
stream.write(0x00);
} catch (IOException e) {
throw new IIOException("I/O error writing Application Extension!", e);
}
}
}
}
private void write_IHDR() throws IOException {
// Write IHDR chunk
ChunkStream cs = new ChunkStream(PNGImageReader.IHDR_TYPE, stream);
cs.writeInt(metadata.IHDR_width);
cs.writeInt(metadata.IHDR_height);
cs.writeByte(metadata.IHDR_bitDepth);
cs.writeByte(metadata.IHDR_colorType);
if (metadata.IHDR_compressionMethod != 0) {
throw new IIOException(
"Only compression method 0 is defined in PNG 1.1");
}
cs.writeByte(metadata.IHDR_compressionMethod);
if (metadata.IHDR_filterMethod != 0) {
throw new IIOException(
"Only filter method 0 is defined in PNG 1.1");
}
cs.writeByte(metadata.IHDR_filterMethod);
if (metadata.IHDR_interlaceMethod < 0 ||
metadata.IHDR_interlaceMethod > 1) {
throw new IIOException(
"Only interlace methods 0 (node) and 1 (adam7) are defined in PNG 1.1");
}
cs.writeByte(metadata.IHDR_interlaceMethod);
cs.finish();
}
private void parse_hIST_chunk(int chunkLength) throws IOException,
IIOException
{
if (!metadata.PLTE_present) {
throw new IIOException("hIST chunk without prior PLTE chunk!");
}
/* According to PNG specification length of
* hIST chunk is specified in bytes and
* hIST chunk consists of 2 byte elements
* (so we expect length is even).
*/
metadata.hIST_histogram = new char[chunkLength/2];
stream.readFully(metadata.hIST_histogram,
0, metadata.hIST_histogram.length);
metadata.hIST_present = true;
}
private void parse_hIST_chunk(int chunkLength) throws IOException,
IIOException
{
if (!metadata.PLTE_present) {
throw new IIOException("hIST chunk without prior PLTE chunk!");
}
/* According to PNG specification length of
* hIST chunk is specified in bytes and
* hIST chunk consists of 2 byte elements
* (so we expect length is even).
*/
metadata.hIST_histogram = new char[chunkLength/2];
stream.readFully(metadata.hIST_histogram,
0, metadata.hIST_histogram.length);
metadata.hIST_present = true;
}
private void skipPass(int passWidth, int passHeight)
throws IOException, IIOException {
if ((passWidth == 0) || (passHeight == 0)) {
return;
}
int inputBands = inputBandsForColorType[metadata.IHDR_colorType];
int bytesPerRow = (inputBands*passWidth*metadata.IHDR_bitDepth + 7)/8;
// Read the image row-by-row
for (int srcY = 0; srcY < passHeight; srcY++) {
// Skip filter byte and the remaining row bytes
pixelStream.skipBytes(1 + bytesPerRow);
// If read has been aborted, just return
// processReadAborted will be called later
if (abortRequested()) {
return;
}
}
}
/**
* Resets the current stream byte and bit positions from the stack
* of marked positions.
*
* <p> An <code>IOException</code> will be thrown if the previous
* marked position lies in the discarded portion of the stream.
*
* @exception IOException if an I/O error occurs.
*/
public void reset() throws IOException {
if (markByteStack.empty()) {
return;
}
long pos = ((Long)markByteStack.pop()).longValue();
if (pos < flushedPos) {
throw new IIOException
("Previous marked position has been discarded!");
}
seek(pos);
int offset = ((Integer)markBitStack.pop()).intValue();
setBitOffset(offset);
}
/**
* Skips <code>count</code> bytes, leaving the buffer
* in an appropriate state. If the end of the stream is
* encountered, an <code>IIOException</code> is thrown with the
* message "Image Format Error".
*/
void skipData(int count) throws IOException {
// First see what's left in the buffer.
if (bufAvail >= count) { // It's enough
bufAvail -= count;
bufPtr += count;
return;
}
if (bufAvail > 0) { // Some there, but not enough
count -= bufAvail;
bufAvail = 0;
bufPtr = 0;
}
// Now read the rest directly from the stream
if (iis.skipBytes(count) != count) {
throw new IIOException ("Image format Error");
}
}
private void writeCommentExtension(GIFWritableImageMetadata im)
throws IOException {
if (im.comments != null) {
try {
Iterator iter = im.comments.iterator();
while (iter.hasNext()) {
stream.write(0x21);
stream.write(0xfe);
writeBlocks((byte[])iter.next());
stream.write(0x00);
}
} catch (IOException e) {
throw new IIOException("I/O error writing Comment Extension!", e);
}
}
}
/**
* Constructor for creating <code>MarkerSegment</code>s by reading
* from an <code>ImageInputStream</code>.
*/
MarkerSegment(JPEGBuffer buffer) throws IOException {
buffer.loadBuf(3); // tag plus length
tag = buffer.buf[buffer.bufPtr++] & 0xff;
length = (buffer.buf[buffer.bufPtr++] & 0xff) << 8;
length |= buffer.buf[buffer.bufPtr++] & 0xff;
length -= 2; // JPEG length includes itself, we don't
if (length < 0) {
throw new IIOException("Invalid segment length: " + length);
}
buffer.bufAvail -= 3;
// Now that we know the true length, ensure that we've got it,
// or at least a bufferful if length is too big.
buffer.loadBuf(length);
}
Qtable(JPEGBuffer buffer) throws IIOException {
elementPrecision = buffer.buf[buffer.bufPtr] >>> 4;
tableID = buffer.buf[buffer.bufPtr++] & 0xf;
if (elementPrecision != 0) {
// IJG is compiled for 8-bits, so this shouldn't happen
throw new IIOException ("Unsupported element precision");
}
data = new int [QTABLE_SIZE];
// Read from zig-zag order to natural order
for (int i = 0; i < QTABLE_SIZE; i++) {
data[i] = buffer.buf[buffer.bufPtr+zigzag[i]] & 0xff;
}
buffer.bufPtr += QTABLE_SIZE;
}
public IIOMetadata getImageMetadata(int imageIndex)
throws IOException {
checkIndex(imageIndex);
if (metadata == null) {
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
}
return metadata;
}
private void writeImageDescriptor(int imageLeftPosition,
int imageTopPosition,
int imageWidth,
int imageHeight,
boolean interlaceFlag,
boolean sortFlag,
int bitsPerPixel,
byte[] localColorTable)
throws IOException {
try {
stream.write(0x2c);
stream.writeShort((short)imageLeftPosition);
stream.writeShort((short)imageTopPosition);
stream.writeShort((short)imageWidth);
stream.writeShort((short)imageHeight);
int packedFields = localColorTable != null ? 0x80 : 0x00;
if (interlaceFlag) {
packedFields |= 0x40;
}
if (sortFlag) {
packedFields |= 0x8;
}
packedFields |= (bitsPerPixel - 1);
stream.write(packedFields);
if (localColorTable != null) {
stream.write(localColorTable);
}
} catch (IOException e) {
throw new IIOException("I/O error writing Image Descriptor!", e);
}
}
public int getHeight(int imageIndex) throws IIOException {
if (imageIndex != 0) {
throw new IndexOutOfBoundsException("imageIndex != 0!");
}
readHeader();
return metadata.IHDR_height;
}
/** Checks that the provided <code>ImageWriter</code> can encode
* the provided <code>ImageTypeSpecifier</code> or not. If not, an
* <code>IIOException</code> will be thrown.
* @param writer The provided <code>ImageWriter</code>.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
public int getWidth(int imageIndex) throws IIOException {
checkIndex(imageIndex);
int index = locateImage(imageIndex);
if (index != imageIndex) {
throw new IndexOutOfBoundsException();
}
readMetadata();
return imageMetadata.imageWidth;
}
public int getWidth(int imageIndex) throws IIOException {
if (imageIndex != 0) {
throw new IndexOutOfBoundsException("imageIndex != 0!");
}
readHeader();
return metadata.IHDR_width;
}
public int getHeight(int imageIndex) throws IOException {
checkIndex(imageIndex);
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
return height;
}
private int locateImage(int imageIndex) throws IIOException {
readHeader();
try {
// Find closest known index
int index = Math.min(imageIndex, imageStartPosition.size() - 1);
// Seek to that position
Long l = (Long)imageStartPosition.get(index);
stream.seek(l.longValue());
// Skip images until at desired index or last image found
while (index < imageIndex) {
if (!skipImage()) {
--index;
return index;
}
Long l1 = new Long(stream.getStreamPosition());
imageStartPosition.add(l1);
++index;
}
} catch (IOException e) {
throw new IIOException("Couldn't seek!", e);
}
if (currIndex != imageIndex) {
imageMetadata = null;
}
currIndex = imageIndex;
return imageIndex;
}
private void parse_sPLT_chunk(int chunkLength)
throws IOException, IIOException {
metadata.sPLT_paletteName = readNullTerminatedString("ISO-8859-1", 80);
chunkLength -= metadata.sPLT_paletteName.length() + 1;
int sampleDepth = stream.readUnsignedByte();
metadata.sPLT_sampleDepth = sampleDepth;
int numEntries = chunkLength/(4*(sampleDepth/8) + 2);
metadata.sPLT_red = new int[numEntries];
metadata.sPLT_green = new int[numEntries];
metadata.sPLT_blue = new int[numEntries];
metadata.sPLT_alpha = new int[numEntries];
metadata.sPLT_frequency = new int[numEntries];
if (sampleDepth == 8) {
for (int i = 0; i < numEntries; i++) {
metadata.sPLT_red[i] = stream.readUnsignedByte();
metadata.sPLT_green[i] = stream.readUnsignedByte();
metadata.sPLT_blue[i] = stream.readUnsignedByte();
metadata.sPLT_alpha[i] = stream.readUnsignedByte();
metadata.sPLT_frequency[i] = stream.readUnsignedShort();
}
} else if (sampleDepth == 16) {
for (int i = 0; i < numEntries; i++) {
metadata.sPLT_red[i] = stream.readUnsignedShort();
metadata.sPLT_green[i] = stream.readUnsignedShort();
metadata.sPLT_blue[i] = stream.readUnsignedShort();
metadata.sPLT_alpha[i] = stream.readUnsignedShort();
metadata.sPLT_frequency[i] = stream.readUnsignedShort();
}
} else {
throw new IIOException("sPLT sample depth not 8 or 16!");
}
metadata.sPLT_present = true;
}
public int getHeight(int imageIndex) throws IOException {
checkIndex(imageIndex);
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
return height;
}
public Iterator getImageTypes(int imageIndex)
throws IOException {
checkIndex(imageIndex);
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
ArrayList list = new ArrayList(1);
list.add(new ImageTypeSpecifier(originalColorModel,
originalSampleModel));
return list.iterator();
}
public IIOMetadata getImageMetadata(int imageIndex)
throws IOException {
checkIndex(imageIndex);
if (metadata == null) {
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
}
return metadata;
}