Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 852 times)
0 Members and 1 Guest are viewing this topic.
Offline Kefwar

Senior Member


Medals: 5
Projects: 1



« Posted 2014-04-27 16: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
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-04-27 16: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
Offline Kefwar

Senior Member


Medals: 5
Projects: 1



« Reply #2 - Posted 2014-04-27 16: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
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-04-27 16: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
« Reply #4 - Posted 2014-04-27 16:32:18 »

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

Myomyomyo.
Offline Kefwar

Senior Member


Medals: 5
Projects: 1



« Reply #5 - Posted 2014-04-27 16:33:48 »

this?

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

Senior Member


Medals: 5
Projects: 1



« Reply #6 - Posted 2014-04-27 16: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
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2014-04-27 16: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
« Reply #8 - Posted 2014-04-27 17: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 Coder


Medals: 40


From rags to riches...to rags.


« Reply #9 - Posted 2014-04-27 17: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
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2014-04-27 17: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
Offline Kefwar

Senior Member


Medals: 5
Projects: 1



« Reply #11 - Posted 2014-04-27 17: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 19: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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (23 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!