This is a REALLY old thread, but it was still the first thing to pop up when I googled the issue. So I just wanted to add this:
InputStream inputStream = conn.getInputStream();
int length = inputStream.available();
Worked for me. And MUCH simpler than the other answers here.
Warning This solution does not provide reliable results regarding the total size of a stream. Except from the JavaDoc:
Answer from W. B. Reed on Stack OverflowNote that while some implementations of {@code InputStream} will return * the total number of bytes in the stream, many will not.
This is a REALLY old thread, but it was still the first thing to pop up when I googled the issue. So I just wanted to add this:
InputStream inputStream = conn.getInputStream();
int length = inputStream.available();
Worked for me. And MUCH simpler than the other answers here.
Warning This solution does not provide reliable results regarding the total size of a stream. Except from the JavaDoc:
Note that while some implementations of {@code InputStream} will return * the total number of bytes in the stream, many will not.
I would read into a ByteArrayOutputStream and then call toByteArray() to get the resultant byte array. You don't need to define the size in advance (although it's possibly an optimisation if you know it. In many cases you won't)
java - How to get the size of an InputStream? - Stack Overflow
java - Get total size of file in bytes - Stack Overflow
Java InputStream size - Stack Overflow
get size of InputStream in Java - Stack Overflow
There is no such thing as the size of an input stream. Consider a program which never exits, or a socket peer which never stops sending. And you don't need to know to write it to an HttpResponse header. The Content-length is managed automatically for you.
Try this
InputStream is = process.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
int b;
while ((b = is.read()) != -1)
os.write(b);
response.setContentLength(os.size());
response.getOutputStream().write(os.toByteArray());
Of a general InputStream? You'd have to just keep reading and reading (e.g. into the same buffer, over and over) counting how many bytes you've read, until you come to the end of the stream.
Of course, you then won't be able to read the data itself... if you want to do that, you'll need to keep the data as you read it, e.g. by copying it to a ByteArrayOutputStream.
(If you're able to process the data at the same time as working out the length, just do that - as you read it using a loop, reading into the same buffer each time, just increment a counter to record how much you've read. You haven't really provided us any information about what you want to do with the stream.)
It isn't knowable in principle (consider a peer that never stops writing), and you don't need it. Just read and write in a loop using a fixed size buffer. NIO doesn't offer any magical solution.
You have to read the whole stream for that. Streams are probably not even complete when you start reading from them, so the size may not be known at that time.
You can output whats in InputStream and get the length
ObjA = new ObjA;
while( more message to read ){
ObjA = inputStreamObj.read();
}
System.out.println(ObjA.length());
There's no way to do this reliably across all FTP services.
The FTP protocol does not provide a way to get file sizes, so you would need to resort to requesting a directory listing and unpicking the (server specific) text that you get back. Furthermore, there is no guarantee that directory listing will be enabled, or that you will have permission to list the directory.
Having said this, some FTP libraries can be configured to attempt to get a file size. For example, with the Apache FTPClient library you can (try to) use the listFiles(String) method and look at the resulting FTPFile object.
EDIT
@Kevin Brock mentions the FTP SIZE command as a possibility.
- This is command was first defined in RFC 3659 in 2007. A lot of deployed FTP server software is much older than that.
- SIZE is an optional command; i.e. even RFC 3659 compliant servers are not required to support it.
- Even if SIZE is supported by a server, the current Apache FTPClient APIs do not include a method for getting the size of a file (apart from via
listFiles). Of course, this may change in the future. - The ftp4j
FTPClientclass apparently does implement SIZE and make it available via itsfileSize(...)method.
I haven't found a simple command to do this either. My solution was to call the skip command, which should return the number of bytes skipped. Sum this until skip returns 0. There is a possibility that skip can return 0 for reasons other than end of file, but in the field it seems to work well.
Here is some example code:
InputStream checkStream = m_ftpClient.retrieveFileStream(name);
long fileLength = 0;
long transferredLength = 0;
do
{
transferredLength = checkStream.skip(MAX_BYTES);
fileLength += transferredLength;
}while(transferredLength > 0);
Hey guys ,so how i can check if an Inputstream got bytes left but without using the read() method, also i need a method to get nextByte also without using read. All i have is a puffer.
A while ago I was reading data from a file, which included a bunch of modules, consisting of a header and a payload. When reading the payloads, I initially had something like this code:
byte[] buffer = new byte[size]; // Size of the payload was from the header
int read = stream.read(buffer); // stream is the InputStream being read
if (read != size) {
// Throw error here
}This ended up having some problems, and as I was using a Java version past Java 9, I just used the readNBytes method.
byte[] bytes = stream.readNBytes(size);
This solved the issues I was having, but brought the question to me, why does this happen? Why does reading to a buffer stop reading accurately if the size of the buffer is very large?
I'm currently writing a program where I am reading a file and performance is relevant, so I would rather use buffering, rather than readNBytes, but at what length does reading with a byte buffer begin to have problems?