下面列出了javax.imageio.stream.ImageInputStream#getStreamPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws IOException {
MarkTryFinallyReproducerSpi spi = new MarkTryFinallyReproducerSpi();
IIORegistry.getDefaultInstance().registerServiceProvider(spi);
ImageInputStream iis1 =
new NotClosingImageInputStream(ImageIO.createImageInputStream(new ByteArrayInputStream(bmp)));
iis1.readByte();
iis1.mark();
long p1 = iis1.getStreamPosition();
iis1.readByte();
iis1.mark();
long p2 = iis1.getStreamPosition();
BufferedImage bi1 = ImageIO.read(iis1);
iis1.reset();
long pn2 = iis1.getStreamPosition();
iis1.reset();
long pn1 = iis1.getStreamPosition();
if (p1 != pn1 || p2!= pn2) {
throw new RuntimeException("Exception from call to canDecodeInput in ImageIO. " +
"Corrupted stack in ImageInputStream");
}
}
private void readData() throws IOException {
if ( fData != null ) {
return;
}
ImageInputStream input = ( ImageInputStream ) getInput();
long length = input.length();
if ( length > Integer.MAX_VALUE ) {
throw new IOException( "Cannot read image of size " + length );
}
if ( input.getStreamPosition() != 0L ) {
if ( isSeekForwardOnly() ) {
throw new IOException();
}
else {
input.seek( 0 );
}
}
byte[] data;
if ( length > 0 ) {
data = new byte[ ( int ) length ];
input.readFully( data );
}
else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[ 4096 ];
int bytesRead;
while ( ( bytesRead = input.read( buffer ) ) != -1 ) {
out.write( buffer, 0, bytesRead );
}
out.close();
data = out.toByteArray();
}
fData = data;
}
private void readData() throws IOException {
if ( fData != null ) {
return;
}
ImageInputStream input = ( ImageInputStream ) getInput();
long length = input.length();
if ( length > Integer.MAX_VALUE ) {
throw new IOException( "Cannot read image of size " + length );
}
if ( input.getStreamPosition() != 0L ) {
if ( isSeekForwardOnly() ) {
throw new IOException();
}
else {
input.seek( 0 );
}
}
byte[] data;
if ( length > 0 ) {
data = new byte[ ( int ) length ];
input.readFully( data );
}
else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[ 4096 ];
int bytesRead;
while ( ( bytesRead = input.read( buffer ) ) != -1 ) {
out.write( buffer, 0, bytesRead );
}
out.close();
data = out.toByteArray();
}
fData = data;
}
private int getAdobeTransformByBruteForce(ImageInputStream iis) throws IOException
{
int a = 0;
iis.seek(0);
int by;
while ((by = iis.read()) != -1)
{
if (ADOBE.charAt(a) == by)
{
++a;
if (a != ADOBE.length())
{
continue;
}
// match
a = 0;
long afterAdobePos = iis.getStreamPosition();
iis.seek(iis.getStreamPosition() - 9);
int tag = iis.readUnsignedShort();
if (tag != 0xFFEE)
{
iis.seek(afterAdobePos);
continue;
}
int len = iis.readUnsignedShort();
if (len >= POS_TRANSFORM + 1)
{
byte[] app14 = new byte[Math.max(len, POS_TRANSFORM + 1)];
if (iis.read(app14) >= POS_TRANSFORM + 1)
{
return app14[POS_TRANSFORM];
}
}
}
else
{
a = 0;
}
}
return 0;
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
private int getAdobeTransformByBruteForce(ImageInputStream iis) throws IOException
{
int a = 0;
iis.seek(0);
int by;
while ((by = iis.read()) != -1)
{
if (ADOBE.charAt(a) == by)
{
++a;
if (a != ADOBE.length())
{
continue;
}
// match
a = 0;
long afterAdobePos = iis.getStreamPosition();
iis.seek(iis.getStreamPosition() - 9);
int tag = iis.readUnsignedShort();
if (tag != 0xFFEE)
{
iis.seek(afterAdobePos);
continue;
}
int len = iis.readUnsignedShort();
if (len >= POS_TRANSFORM + 1)
{
byte[] app14 = new byte[Math.max(len, POS_TRANSFORM + 1)];
if (iis.read(app14) >= POS_TRANSFORM + 1)
{
return app14[POS_TRANSFORM];
}
}
}
else
{
a = 0;
}
}
return 0;
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream)source;
stream.mark();
try {
int type = stream.readByte(); // TypeField
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// We can't verify that amount of data in the stream
// corresponds to image dimension because we do not know
// the length of the data stream.
// Assuming that wbmp image are used for mobile devices,
// let's introduce an upper limit for image dimension.
// In case if exact amount of raster data is unknown,
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}
public SubImageInputStream(ImageInputStream stream, int length)
throws IOException {
this.stream = stream;
this.startingPos = stream.getStreamPosition();
this.startingLength = this.length = length;
}