Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

URL FileSize does not match to the Real File size


  • Please log in to reply
20 replies to this topic

#1 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 11 February 2012 - 12:43 PM

Hello, how are you? :)


A have just uploaded my "a.mp3" to my ftp;.
In my Computer it is 8,888,890 bytes, and the same is shown in a ftp file client.
I've downloaded the file and it's really 8,888,890 bytes. ok, ok , everything is all right but... but,

Now I've tested it in a Java:



InputStream in; // read from the HTTP
FileInputStream in2; // read from my HDD


try{
in2 = new FileInputStream("a.mp3"); // that's my file
URL url1 = new URL("http://something.ge/a.mp3"); // my file in my web page
in = (url1.openStream());
System.out.println(in.available() +", "+ in2.available());
}catch(Exception e){
System.out.println("Ooops!");
}


output is this:
8475, 8888890



And when I try to download it from Java, it's less that it is really;


FileInputStream in2;
FileOutputStream out;

try{
URL url1 = new URL("http://somethin/a.mp3");
in = (url1.openStream());
out = new FileOutputStream("new.mp3");

byte[] buffer = new byte[1024];
int r=0;
while(r<in.available()){
in.read(buffer);
out.write(buffer);
}

}catch(Exception e){
System.out.println("Oooooo");
}



what's happening here, could you help me please?
  • 0
GNU/Linux Is the Best.

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2,574 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java, PHP

Posted 11 February 2012 - 12:51 PM

I highlighted it :)

public int available()
throws IOException
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.

Overrides:
available in class InputStream
Returns:
an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking.
Throws:
IOException - if this file input stream has been closed by calling close or an I/O error occurs.

For your while loop, just keep on reading untill you read null instead of checking with available();
  • 0

#3 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 11 February 2012 - 02:38 PM

Also you need to use the value returned by the read() method. It's possible to read fewer bytes than the size of the array.
  • 0

#4 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 12 February 2012 - 09:57 AM

wim DC
Norm

thanks for this answer. :)

I'm writing a Download Manager Like FashGet or Internet Download Manager. it splits the bytes of a file and downloads partition of the file together (each partition in each Thread);

So I must know the real file size, before I start downloading. how can i do this, could you tell me please?
  • 0
GNU/Linux Is the Best.

#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 12 February 2012 - 10:03 AM

Does the server have a way you can ask it the size of the file?
If you are using HTTP, the content-length field in the response header might have the length.
  • 0

#6 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 12 February 2012 - 10:14 AM

Does the server have a way you can ask it the size of the file?
If you are using HTTP, the content-length field in the response header might have the length.


I'dont Know how to check this. Could you tell me, please? I want the method that will work everywhere :)
that's my Audio on HTTP:


  • 0
GNU/Linux Is the Best.

#7 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 12 February 2012 - 10:18 AM

When you send a HTTP request to a server, the server's response starts with a header block followed by the data. The header contains a field: content-length. You should get that field and look at its value to see if it is what you need.

The URL class has several methods that could help you get the contents of the response header. Try using them and printing what is returned.

I have a utility program I use to download files. When I executed it I got this message:
Copying: a.mp3 ... 8888890 bytes

It must have gotten the length from the response header. I'll check.
  • 0

#8 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 12 February 2012 - 10:25 AM

The URL class has several methods that could help you get the contents of the response header. Try using them and printing what is returned. I have a utility program I use to download files. When I executed it I got this message:
Copying: a.mp3 ... 8888890 bytes
It must have gotten the length from the response header. I'll check.


Yes, that's real size of my file. Could you tell me one of the method that sends me the real bytes of the file?
  • 0
GNU/Linux Is the Best.

#9 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 12 February 2012 - 10:26 AM

Did you read the API doc for the URL class. I'm sure that it is there.
  • 0

#10 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 12 February 2012 - 10:41 AM

Oh, I've done it!

HttpURLConnection conn = null;
        try {
            URL url = new URL("Http://teodore.ge/a.mp3");
			conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.getInputStream();
            System.out.println(conn.getContentLength());
        } catch (IOException e) {
            
        } finally {
        	conn.disconnect();
        }

Result is: 8888890


:)
  • 0
GNU/Linux Is the Best.

#11 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2,574 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java, PHP

Posted 12 February 2012 - 10:44 AM

You should be able to split it into partitions using the estimated size, but let the thread that reads the final bit make sure it reads it fully, not caring what the guessed size was.

Edit: okay didn't quite refresh my page in the past half hour to see the responses I missed ^^
  • 0

#12 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 12 February 2012 - 10:53 AM

Thanks everyone! I love codecall.net.
I'll soon finish my Project, and I'll show you :)
  • 0
GNU/Linux Is the Best.