0

Hi StackOverflow family,

My aim is to get the contents of an URL file in a given range. In order to do this, I have two bytes, one is starting byte, other one is end byte.

However, I don't know how to do this. I am actually reading byte by byte by using a byte array.

I added explanations on the code, thanks.

Here is my code:

        // Scenario - 1
        if(args.length == 3){ // 3 OLACAK
            con.setRequestMethod("GET");

            fromURL = new BufferedInputStream(con.getInputStream(), bufSize);
            toFile = new BufferedOutputStream(new FileOutputStream(outputFile), bufSize);   

            if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
                // READING BYTE BY BYTE HERE
                int read = -1;
                byte[] buf = new byte[bufSize];
                while ((read = fromURL.read(buf, 0, bufSize)) >= 0) {
                    toFile.write(buf, 0, read);
                }
                toFile.close();
                System.out.println("ok");
            }
        // Scenario - 2
        }else if(args.length == 0){
            con.setRequestMethod("HEAD");

            if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
                byte startRange =  0; //Integer.parseInt(args[3]);
                byte finishRange =  25;//Integer.parseInt(args[4]);


                System.out.println(con.getContentLength());
                if(startRange < 0 || finishRange > ((byte)con.getContentLength())){
                    System.out.println("Range is not OK.");
                }else{                     

                    int read = -1;
                    byte[] buf = new byte[finishRange];

                    // HOW TO INCLUDE START AND END BYTES TO THIS PART OF CODE??????
                    //////////////////////////////////////////////////////////////
                    while ((read = fromURL.read(buf, 0, finishRange)) >= startRange) {
                        //toFile.write(buf, 0, read);
                    }
                    toFile.close();

                    System.out.println("AAA");   
                }  
            }
        }else{
            System.out.println("Wrong argument count.");
        }

1 Answer 1

0

Assuming you want to read the bytes in range [startRange, finishRange], one solution would be to skip the starting bytes, and then to read the next finishRange in the InputStream.

long skipped = fromURL.skip(startRange);
if(skipped == startRange) {
   // OK There was enough bytes to be skipped
   if((read = fromURL.read(buf, 0, finishRange)) > -1) {
      //toFile.write(buf, 0, read);
   }
} else {
   // TODO Handle error when there was not enough bytes to be skipped
}
2
  • thanks for your answer, but i am always get the skipped variable as 0 whatever i wrote into startRange. How could that happen? Mar 9, 2013 at 12:44
  • Does this happen for scenario 1 or 2?
    – aymeric
    Mar 9, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.