problems with ImageIO

R

Ron

The following code does not work ("file" and "image" are members of the
class; nextNumber(InputStream) returns the next number in the
InputStream):

public void write(PrintStream stream) throws IOException {
FileInputStream in = new FileInputStream(file);
int size = in.available();
stream.println(size);
byte[] b = new byte[in.available()];
in.read(b);
stream.write(b, 0, size);
in.close();
}

public void read(InputStream stream) throws IOException {
int size = (int)nextNumber(stream);
byte[] buf = new byte[size];
stream.read(buf, 0, size);
ByteArrayInputStream imageStream = new ByteArrayInputStream(buf);
image = ImageIO.read(imageStream);
}

"image" is null when I run this code. Suffice to say, the code
"ImageIO.read(file)" works. Can anyone see a problem with this? Thanks.

Ron
 
K

Knute Johnson

Ron said:
The following code does not work ("file" and "image" are members of the
class; nextNumber(InputStream) returns the next number in the
InputStream):

public void write(PrintStream stream) throws IOException {
FileInputStream in = new FileInputStream(file);
int size = in.available();
stream.println(size);
byte[] b = new byte[in.available()];
in.read(b);
stream.write(b, 0, size);
in.close();
}

public void read(InputStream stream) throws IOException {
int size = (int)nextNumber(stream);
byte[] buf = new byte[size];
stream.read(buf, 0, size);
ByteArrayInputStream imageStream = new ByteArrayInputStream(buf);
image = ImageIO.read(imageStream);
}

"image" is null when I run this code. Suffice to say, the code
"ImageIO.read(file)" works. Can anyone see a problem with this? Thanks.

Ron

From the docs:

"ImageIO.read(InputStream is)

Returns:
a BufferedImage containing the decoded contents of the input, or null."
 
A

Andrey Kuznetsov

The following code does not work ("file" and "image" are members of the
class; nextNumber(InputStream) returns the next number in the
InputStream):

public void write(PrintStream stream) throws IOException {
FileInputStream in = new FileInputStream(file);
int size = in.available();
stream.println(size);
byte[] b = new byte[in.available()];
in.read(b);
stream.write(b, 0, size);
in.close();
}

what do you want with PrintStream?
note also that available gives you only how much bytes may be read without
blocking, NOT full size of data behind stream.
public void read(InputStream stream) throws IOException {
int size = (int)nextNumber(stream);
byte[] buf = new byte[size];
stream.read(buf, 0, size);
ByteArrayInputStream imageStream = new ByteArrayInputStream(buf);
image = ImageIO.read(imageStream);
}

similar problem here: InputStream.read(buf, 0, size) returns how much byte
were read.
Use BufferedInputStream.readFully().

BTW why don't you create image direct from supplied stream?

public void read(InputStream stream) throws IOException {
image = ImageIO.read(stream);
}

Andrey
 
R

Ron

note also that available gives you only how much bytes may be read without
blocking, NOT full size of data behind stream.

I'm sure that it's giving me the full size of the file. It returns 1283
and the size of the file is 1283.
BTW why don't you create image direct from supplied stream?

Because the stream has more data than that of the image. I'm developing
a graphics program that has the ability to import images. I want the
files that my program to generate to imbed these images within the
file. So, the files look like this:

File header
Object 1 data...
Object 2 data...
Imbedded image header
[imbedded image data]
Object 3 data...
....
 
R

Ron

Nevermind... the problem was that I wasn't reading in the newline
character created after stream.println(size) before I did in.read(b).
It works perfectly now. Thanks for the help anyway.

Andrey said:
The following code does not work ("file" and "image" are members of the
class; nextNumber(InputStream) returns the next number in the
InputStream):

public void write(PrintStream stream) throws IOException {
FileInputStream in = new FileInputStream(file);
int size = in.available();
stream.println(size);
byte[] b = new byte[in.available()];
in.read(b);
stream.write(b, 0, size);
in.close();
}

what do you want with PrintStream?
note also that available gives you only how much bytes may be read without
blocking, NOT full size of data behind stream.
public void read(InputStream stream) throws IOException {
int size = (int)nextNumber(stream);
byte[] buf = new byte[size];
stream.read(buf, 0, size);
ByteArrayInputStream imageStream = new ByteArrayInputStream(buf);
image = ImageIO.read(imageStream);
}

similar problem here: InputStream.read(buf, 0, size) returns how much byte
were read.
Use BufferedInputStream.readFully().

BTW why don't you create image direct from supplied stream?

public void read(InputStream stream) throws IOException {
image = ImageIO.read(stream);
}

Andrey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top