Shouldn't we keep track of the offset when we write to an OutputStream?












0















The read() method inside copyFile() reads buf.length bytes from the input stream and then it writes them to the output stream from the start until len.



public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);

}
out.close();
inputStream.close();
} catch (IOException e) {
return false;
}
return true;
}


If we always writing to the output stream from the start isn't the data of the previous iteration overwritten?



Don't we need to keep track of the offset? For example if the first iteration wrote 1024 bytes then the second iteration should write out.write(buf,1024,len);.










share|improve this question




















  • 2





    The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

    – Arnaud
    Nov 22 '18 at 13:09











  • Thanks @Arnaud!

    – Skemelio
    Nov 22 '18 at 13:10
















0















The read() method inside copyFile() reads buf.length bytes from the input stream and then it writes them to the output stream from the start until len.



public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);

}
out.close();
inputStream.close();
} catch (IOException e) {
return false;
}
return true;
}


If we always writing to the output stream from the start isn't the data of the previous iteration overwritten?



Don't we need to keep track of the offset? For example if the first iteration wrote 1024 bytes then the second iteration should write out.write(buf,1024,len);.










share|improve this question




















  • 2





    The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

    – Arnaud
    Nov 22 '18 at 13:09











  • Thanks @Arnaud!

    – Skemelio
    Nov 22 '18 at 13:10














0












0








0








The read() method inside copyFile() reads buf.length bytes from the input stream and then it writes them to the output stream from the start until len.



public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);

}
out.close();
inputStream.close();
} catch (IOException e) {
return false;
}
return true;
}


If we always writing to the output stream from the start isn't the data of the previous iteration overwritten?



Don't we need to keep track of the offset? For example if the first iteration wrote 1024 bytes then the second iteration should write out.write(buf,1024,len);.










share|improve this question
















The read() method inside copyFile() reads buf.length bytes from the input stream and then it writes them to the output stream from the start until len.



public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);

}
out.close();
inputStream.close();
} catch (IOException e) {
return false;
}
return true;
}


If we always writing to the output stream from the start isn't the data of the previous iteration overwritten?



Don't we need to keep track of the offset? For example if the first iteration wrote 1024 bytes then the second iteration should write out.write(buf,1024,len);.







java io inputstream outputstream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:02









Micha Wiedenmann

10.3k1364103




10.3k1364103










asked Nov 22 '18 at 13:00









SkemelioSkemelio

751414




751414








  • 2





    The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

    – Arnaud
    Nov 22 '18 at 13:09











  • Thanks @Arnaud!

    – Skemelio
    Nov 22 '18 at 13:10














  • 2





    The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

    – Arnaud
    Nov 22 '18 at 13:09











  • Thanks @Arnaud!

    – Skemelio
    Nov 22 '18 at 13:10








2




2





The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

– Arnaud
Nov 22 '18 at 13:09





The start is the start of the array, not of the stream. The data is in fact appended to the previous one each time.

– Arnaud
Nov 22 '18 at 13:09













Thanks @Arnaud!

– Skemelio
Nov 22 '18 at 13:10





Thanks @Arnaud!

– Skemelio
Nov 22 '18 at 13:10












2 Answers
2






active

oldest

votes


















0














The fact is that the buf is a buffer and not the entire data stream.



Also, you are using public int read(byte b) method which means which it is same as read(b, 0, b.length). So the buffer shall be pointing to next buf.length values of the data.



For more information, please check https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte) .






share|improve this answer
























  • Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

    – Skemelio
    Nov 22 '18 at 13:14



















0














As @Arnaud commented




The start is the start of the array, not of the stream. The data is in
fact appended to the previous one each time.




I was not careful quick scanning the docs and from "Writes len bytes from the specified byte array starting at offset off to this output stream", I got the point that off was the offset of the stream.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431620%2fshouldnt-we-keep-track-of-the-offset-when-we-write-to-an-outputstream%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The fact is that the buf is a buffer and not the entire data stream.



    Also, you are using public int read(byte b) method which means which it is same as read(b, 0, b.length). So the buffer shall be pointing to next buf.length values of the data.



    For more information, please check https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte) .






    share|improve this answer
























    • Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

      – Skemelio
      Nov 22 '18 at 13:14
















    0














    The fact is that the buf is a buffer and not the entire data stream.



    Also, you are using public int read(byte b) method which means which it is same as read(b, 0, b.length). So the buffer shall be pointing to next buf.length values of the data.



    For more information, please check https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte) .






    share|improve this answer
























    • Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

      – Skemelio
      Nov 22 '18 at 13:14














    0












    0








    0







    The fact is that the buf is a buffer and not the entire data stream.



    Also, you are using public int read(byte b) method which means which it is same as read(b, 0, b.length). So the buffer shall be pointing to next buf.length values of the data.



    For more information, please check https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte) .






    share|improve this answer













    The fact is that the buf is a buffer and not the entire data stream.



    Also, you are using public int read(byte b) method which means which it is same as read(b, 0, b.length). So the buffer shall be pointing to next buf.length values of the data.



    For more information, please check https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte) .







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 13:12









    CS_noobCS_noob

    4071311




    4071311













    • Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

      – Skemelio
      Nov 22 '18 at 13:14



















    • Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

      – Skemelio
      Nov 22 '18 at 13:14

















    Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

    – Skemelio
    Nov 22 '18 at 13:14





    Thanks for your answer. My query was about write() but it's ok it's being answered in the comments.

    – Skemelio
    Nov 22 '18 at 13:14













    0














    As @Arnaud commented




    The start is the start of the array, not of the stream. The data is in
    fact appended to the previous one each time.




    I was not careful quick scanning the docs and from "Writes len bytes from the specified byte array starting at offset off to this output stream", I got the point that off was the offset of the stream.






    share|improve this answer






























      0














      As @Arnaud commented




      The start is the start of the array, not of the stream. The data is in
      fact appended to the previous one each time.




      I was not careful quick scanning the docs and from "Writes len bytes from the specified byte array starting at offset off to this output stream", I got the point that off was the offset of the stream.






      share|improve this answer




























        0












        0








        0







        As @Arnaud commented




        The start is the start of the array, not of the stream. The data is in
        fact appended to the previous one each time.




        I was not careful quick scanning the docs and from "Writes len bytes from the specified byte array starting at offset off to this output stream", I got the point that off was the offset of the stream.






        share|improve this answer















        As @Arnaud commented




        The start is the start of the array, not of the stream. The data is in
        fact appended to the previous one each time.




        I was not careful quick scanning the docs and from "Writes len bytes from the specified byte array starting at offset off to this output stream", I got the point that off was the offset of the stream.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 13:28

























        answered Nov 22 '18 at 13:19









        SkemelioSkemelio

        751414




        751414






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431620%2fshouldnt-we-keep-track-of-the-offset-when-we-write-to-an-outputstream%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python