Shouldn't we keep track of the offset when we write to an OutputStream?
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
add a comment |
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
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
add a comment |
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
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
java io inputstream outputstream
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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) .
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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) .
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
add a comment |
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) .
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
add a comment |
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) .
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) .
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 22 '18 at 13:28
answered Nov 22 '18 at 13:19
SkemelioSkemelio
751414
751414
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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