Home arrow static arrow Java Programming [Archive] - getting contentlength
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - getting contentlength
This topic has 22 replies on 2 pages.    « Previous | 1 | 2 |

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 5:47 AM (reply 15 of 22)



 
And, you have to add the decoded bytes to another buffer than the one you're using for input.

If you look at my original code-post you'll see that I use "somebytebuffer" and "somebuffer", these are different objects.
 

Posts:21,719
Registered: 98-02-20
Re: getting contentlength  
Jul 20, 2004 5:58 AM (reply 16 of 22)



 
Maybe something like this can help. Look at the getContents method.

import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.IOException;import java.io.PrintStream; public class ReaderWriterTest{    public static final int DEFAULT_BUFFER_SIZE = 2048;     public static void main(String [] args)    {        try        {            String inputFileName    = ((args.length > 0) ? args[0] : "ReaderWriterTest.java");            ReaderWriterTest rwt    = new ReaderWriterTest();            String contents         = rwt.getContents(new FileInputStream(inputFileName));            System.out.println("content length: " + contents.length());            PrintStream ps          = ((args.length > 1) ? new PrintStream(new FileOutputStream(args[1])) : System.out);            ps.println(contents);        }        catch (Exception e)        {            e.printStackTrace();        }    }     public String getContents(InputStream is) throws IOException    {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        byte [] buffer = new byte[DEFAULT_BUFFER_SIZE];         while (is.read(buffer) != -1)        {            baos.write(buffer);        }         is.close();         return baos.toString().trim();    }}


%
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 9:30 PM (reply 17 of 22)



 
HI gud morning,
i am held up with the same problem. i tried giving code like this.i get "decode undefined for ByteBuffer"

protected void resumeNotSupportedWork() throws Exception,ModuleException
{
byte [] byteBuffer = new byte[14550] ;
byte[] someBuffer = new byte[14550];
Charset cset = Charset.forName(rsp.getHeader("Content-Encoding"));

if (cset ==null)
cset = Charset.forName("UTF-8");
int readBytes = ipStream.read(byteBuffer,0,1024);
if(readBytes==0)throw new IOException("read 0 bytes");
else if(readBytes == -1)
break;
else
someBuffer.append(cset.decode(ByteBuffer.wrap(byteBuffer,0,readBytes)));
aThread.setContentLength( rsp.getHeaderAsInt("ContentLength") );
aThread.setResumeSupported(false);

}//end method

this is my function where i should set the contentlength of file to be downloaded to athread.

I get content length for resume supported ones.i am using HTTPClient and its functions.so dunno the internal implementation.

Plz help me out
Thank u
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 9:34 PM (reply 18 of 22)



 
Reading bytes is inside a while(true) loop
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 21, 2004 12:14 AM (reply 19 of 22)



 
As you surely know, arrays do not have methods, so your
 byte[] somebuffer = new byte[...]; somebuffer.append(...);

won't work, so you'll have to use
System.arraycopy
to copy the bytes from somebytebuffer to somebuffer. Or you start using the NIO buffers instead, simply returning the Buffer returned from
CharSet.decode(...)
.
 

Posts:21,719
Registered: 98-02-20
Re: getting contentlength  
Jul 21, 2004 5:38 AM (reply 20 of 22)



 
Why do you need a separate thread?

%
 

Posts:24,036
Registered: 2/3/03
Re: getting contentlength  
Jul 21, 2004 5:46 AM (reply 21 of 22)



 
As you surely know, arrays do not have methods...

pshaw.
int[] array = {1, 2, 3, 4, 5};System.out.println(array.getClass());System.out.println(array.hashCode());System.out.println(array.equals(new Object()));System.out.println(array.toString());


:oP

</pedantry>
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 21, 2004 5:57 AM (reply 22 of 22)



 
Haha,

depends on the way you look at it ;D

Since it's only methods inherited from Object I'd have to rephrase myself.

"Arrays do not have any other methods than those inherited from java.lang.Object"

Forgive me for my premature statement ;D
 
This topic has 22 replies on 2 pages.    « Previous | 1 | 2 |