inputstream length?

G

Grzegorz Stasica

hi,

How can I get length of inputstrema. The code is as follows:

InputStream r=new FileInputStream("test.txt");

I need to know the length of inputstream in order to store in in
database like
preparedstatement.setBinaryStream(param_id,stream, length?)

Rgs
 
O

Oscar kind

Grzegorz Stasica said:
How can I get length of inputstrema.

You don't, as an input stream has the potention to be unlimited.
But there are other possibilities.

The code is as follows:

InputStream r=new FileInputStream("test.txt");

I need to know the length of inputstream in order to store in in
database like
preparedstatement.setBinaryStream(param_id,stream, length?)

This makes it easier: you know the data comes from a file, which has a
length. You can use that length (e.g. through File#length()).
 
G

Grzegorz Stasica

Oscar said:
You don't, as an input stream has the potention to be unlimited.
But there are other possibilities.





This makes it easier: you know the data comes from a file, which has a
length. You can use that length (e.g. through File#length()).
I found method available which return numbers of bytes that can be read
without blocking. What blocking means in this situation?
 
A

Andrey Kuznetsov

InputStream r=new FileInputStream("test.txt");
File f = new File("test.txt");
InputStream r=new FileInputStream(f);
long length = f.length();
 
B

Boudewijn Dijkstra

Grzegorz Stasica said:
hi,

How can I get length of inputstrema. The code is as follows:

InputStream r=new FileInputStream("test.txt");

long length = 0L;
do
long skip = r.skip(Long.MAX_VALUE);
length += skip;
while (skip > 0L)
r = new FileInputStream("test.txt"); // reset the stream
 
S

Steve Horsley

Grzegorz said:
I found method available which return numbers of bytes that can be read
without blocking. What blocking means in this situation?

If a read method doesn't return immediately but waits for more
data to become available, that is blocking. It typically happens
for network connections (sockets) but also to a lesser extent for
network mounted file shares. This is not what you are looking for.

Use File and length, like this:

File f = new File("data.dat");
int length = f.length();

Steve
 
S

Steve Horsley

Whoops: As others have pointed out, File.length() returns a long,
not an int.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top