Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (731)
Games in Android Showcase (217)
games submitted by our members
Games in WIP (799)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Available bytes in InputStream  (Read 4217 times)
0 Members and 1 Guest are viewing this topic.
Online Kefwar
« Posted 2014-04-27 14:18:36 »

Hello JGO,

I would like some suggestions for a fast and reliable way of getting the available bytes in an inputstream.
1  
InputStream.available()
doesn't return the correct value.

I have found a way, but for me it looks really inefficient:
1  
2  
3  
4  
5  
6  
7  
8  
9  
         //"is" is the InputStream
         is.mark(Integer.MAX_VALUE);
         long filesize = 0;
         int len;
         byte[] buffer = new byte[1024];
         while((len = is.read(buffer)) != -1) {
            filesize+= len;
         }
         is.reset();


Any suggestions/improvements are welcome

Offline Riven
Administrator

« JGO Overlord »


Medals: 1284
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-04-27 14:22:25 »

It returns the right value, just read the specs.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Online Kefwar
« Reply #2 - Posted 2014-04-27 14:28:18 »

It returns the right value, just read the specs.
It returns the right value after I've executed the code I posted, but before that, it returns much less.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
         System.out.println(is.available());
         is.mark(Integer.MAX_VALUE);
         long filesize = 0;
         int len;
         byte[] buffer = new byte[1024];
         while((len = is.read(buffer)) != -1) {
            filesize+= len;
         }
         is.reset();
         System.out.println(is.available() + " is " + filesize);

The log:
1  
2  
8192
168262 is 168262

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1284
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-04-27 14:29:29 »

Read the spec, I tell you.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline theagentd

« JGO Bitwise Duke »


Medals: 983
Projects: 4
Exp: 8 years



« Reply #4 - Posted 2014-04-27 14:32:18 »

Yeah, Riven's right. Read the spec.  Tongue

Myomyomyo.
Online Kefwar
« Reply #5 - Posted 2014-04-27 14:33:48 »

this?

If you mean that, I've read it many times, but still don't see a solution

Online Kefwar
« Reply #6 - Posted 2014-04-27 14:55:36 »

I put the question wrong: Available bytes in InputStream should be total number of bytes in InputStream.  Wink
Like downloading a file, how to get the size of that file?

Offline Riven
Administrator

« JGO Overlord »


Medals: 1284
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2014-04-27 14:56:28 »

Parse the HTTP header

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 484
Exp: 7 years



« Reply #8 - Posted 2014-04-27 15:11:39 »

If you want the size of an actual
File
,
Files.size()
works on Java 7+.

You can't really know how large a raw InputStream is until you've read it, it could be infinite for all the code knows, hence the name Stream.
You can either measure something that has a known finite and determined size (e.g. a File), or you can attempt to infer the data from elsewhere (e.g. the HTTP header).
Offline CodeHead

JGO Knight


Medals: 52


From rags to riches...to rags.


« Reply #9 - Posted 2014-04-27 15:23:13 »

HTTP headers may still not deliver the desired information under certain circumstances such as when Chunked Transfer Encoding is used.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1284
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2014-04-27 15:45:47 »

HTTP headers may still not deliver the desired information under certain circumstances such as when Chunked Transfer Encoding is used.
Yeah sure, but Kefwar was talking about file downloads, in which case it is safe to assume the Content-Length line is defined.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Online Kefwar
« Reply #11 - Posted 2014-04-27 15:51:16 »

HTTP headers may still not deliver the desired information under certain circumstances such as when Chunked Transfer Encoding is used.
Yeah sure, but Kefwar was talking about file downloads, in which case it is safe to assume the Content-Length line is defined.

Thanks for the suggestion, I've worked it out and it works.
But since I'm already requesting an information file from the server, I'll add the filesize in that file. This way I can still use FTP protocol.

EDIT: for those who are interested in it, here is the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   private int contentLength(URL url) {
      HttpURLConnection connection;
      int contentLength = -1;

      try {
         HttpURLConnection.setFollowRedirects(false);

         connection = (HttpURLConnection) url.openConnection();
         connection.setRequestMethod("HEAD");

         contentLength = connection.getContentLength();
      } catch (Exception e) {
      }

      return contentLength;
   }

Offline Roquen
« Reply #12 - Posted 2014-04-27 17:42:06 »

I put the question wrong: Available bytes in InputStream should be total number of bytes in InputStream.  Wink
Like downloading a file, how to get the size of that file?
Just to be snarky.  Not only does the docs clearly state the meaning of the result..it also matches the English def of the word available.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Archive (341 views)
2017-04-27 17:45:51

buddyBro (538 views)
2017-04-05 03:38:00

CopyableCougar4 (987 views)
2017-03-24 15:39:42

theagentd (1020 views)
2017-03-24 15:32:08

Rule (995 views)
2017-03-19 12:43:22

Rule (978 views)
2017-03-19 12:42:17

Rule (975 views)
2017-03-19 12:36:21

theagentd (1075 views)
2017-03-16 05:07:07

theagentd (996 views)
2017-03-15 22:37:06

theagentd (771 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!