SequenceInputStream basic test failing












-2















Believe it or not, this test fails to pass on my end. It's my first time working with SequenceInputStream and I wanted to test its functionality. From this test, it seems like completeIs only gets to stream the contents from is2. For the tests to pass is2 & is1 need to be concatenated.



public void testCombineInputStreams() throws IOException {
StringBuffer strBuffer = new StringBuffer();
List<InputStream> isList = new ArrayList();

String charSet = "UTF-8";

String str2 = "world!";
strBuffer.append(str2);
InputStream is2 = new ByteArrayInputStream(str2.getBytes(charSet));
isList.add(is2);

String str1 = "hello";
strBuffer.append(str1);
InputStream is1 = new ByteArrayInputStream(str1.getBytes(charSet));
isList.add(is1);

SequenceInputStream completeIs = new SequenceInputStream(new Vector(isList).elements());

String completeStr = strBuffer.toString();

int expectedNumBytesRead = completeStr.getBytes(charSet).length;
byte readStr = new byte[expectedNumBytesRead];

assertEquals(expectedNumBytesRead, completeIs.available());
completeIs.read(readStr, 0, expectedNumBytesRead);
assertEquals(completeStr, new String(readStr, charSet));
}


Is there anything I've missed here?










share|improve this question

























  • What SequenceInputStream? I see no SequenceInputStream in the code.

    – Andreas
    Nov 21 '18 at 21:10













  • What is completeIs? What is point of isList when you never use it?

    – Andreas
    Nov 21 '18 at 21:15











  • @Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

    – MrBuggy
    Nov 21 '18 at 21:40
















-2















Believe it or not, this test fails to pass on my end. It's my first time working with SequenceInputStream and I wanted to test its functionality. From this test, it seems like completeIs only gets to stream the contents from is2. For the tests to pass is2 & is1 need to be concatenated.



public void testCombineInputStreams() throws IOException {
StringBuffer strBuffer = new StringBuffer();
List<InputStream> isList = new ArrayList();

String charSet = "UTF-8";

String str2 = "world!";
strBuffer.append(str2);
InputStream is2 = new ByteArrayInputStream(str2.getBytes(charSet));
isList.add(is2);

String str1 = "hello";
strBuffer.append(str1);
InputStream is1 = new ByteArrayInputStream(str1.getBytes(charSet));
isList.add(is1);

SequenceInputStream completeIs = new SequenceInputStream(new Vector(isList).elements());

String completeStr = strBuffer.toString();

int expectedNumBytesRead = completeStr.getBytes(charSet).length;
byte readStr = new byte[expectedNumBytesRead];

assertEquals(expectedNumBytesRead, completeIs.available());
completeIs.read(readStr, 0, expectedNumBytesRead);
assertEquals(completeStr, new String(readStr, charSet));
}


Is there anything I've missed here?










share|improve this question

























  • What SequenceInputStream? I see no SequenceInputStream in the code.

    – Andreas
    Nov 21 '18 at 21:10













  • What is completeIs? What is point of isList when you never use it?

    – Andreas
    Nov 21 '18 at 21:15











  • @Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

    – MrBuggy
    Nov 21 '18 at 21:40














-2












-2








-2








Believe it or not, this test fails to pass on my end. It's my first time working with SequenceInputStream and I wanted to test its functionality. From this test, it seems like completeIs only gets to stream the contents from is2. For the tests to pass is2 & is1 need to be concatenated.



public void testCombineInputStreams() throws IOException {
StringBuffer strBuffer = new StringBuffer();
List<InputStream> isList = new ArrayList();

String charSet = "UTF-8";

String str2 = "world!";
strBuffer.append(str2);
InputStream is2 = new ByteArrayInputStream(str2.getBytes(charSet));
isList.add(is2);

String str1 = "hello";
strBuffer.append(str1);
InputStream is1 = new ByteArrayInputStream(str1.getBytes(charSet));
isList.add(is1);

SequenceInputStream completeIs = new SequenceInputStream(new Vector(isList).elements());

String completeStr = strBuffer.toString();

int expectedNumBytesRead = completeStr.getBytes(charSet).length;
byte readStr = new byte[expectedNumBytesRead];

assertEquals(expectedNumBytesRead, completeIs.available());
completeIs.read(readStr, 0, expectedNumBytesRead);
assertEquals(completeStr, new String(readStr, charSet));
}


Is there anything I've missed here?










share|improve this question
















Believe it or not, this test fails to pass on my end. It's my first time working with SequenceInputStream and I wanted to test its functionality. From this test, it seems like completeIs only gets to stream the contents from is2. For the tests to pass is2 & is1 need to be concatenated.



public void testCombineInputStreams() throws IOException {
StringBuffer strBuffer = new StringBuffer();
List<InputStream> isList = new ArrayList();

String charSet = "UTF-8";

String str2 = "world!";
strBuffer.append(str2);
InputStream is2 = new ByteArrayInputStream(str2.getBytes(charSet));
isList.add(is2);

String str1 = "hello";
strBuffer.append(str1);
InputStream is1 = new ByteArrayInputStream(str1.getBytes(charSet));
isList.add(is1);

SequenceInputStream completeIs = new SequenceInputStream(new Vector(isList).elements());

String completeStr = strBuffer.toString();

int expectedNumBytesRead = completeStr.getBytes(charSet).length;
byte readStr = new byte[expectedNumBytesRead];

assertEquals(expectedNumBytesRead, completeIs.available());
completeIs.read(readStr, 0, expectedNumBytesRead);
assertEquals(completeStr, new String(readStr, charSet));
}


Is there anything I've missed here?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:39







MrBuggy

















asked Nov 21 '18 at 20:52









MrBuggyMrBuggy

517




517













  • What SequenceInputStream? I see no SequenceInputStream in the code.

    – Andreas
    Nov 21 '18 at 21:10













  • What is completeIs? What is point of isList when you never use it?

    – Andreas
    Nov 21 '18 at 21:15











  • @Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

    – MrBuggy
    Nov 21 '18 at 21:40



















  • What SequenceInputStream? I see no SequenceInputStream in the code.

    – Andreas
    Nov 21 '18 at 21:10













  • What is completeIs? What is point of isList when you never use it?

    – Andreas
    Nov 21 '18 at 21:15











  • @Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

    – MrBuggy
    Nov 21 '18 at 21:40

















What SequenceInputStream? I see no SequenceInputStream in the code.

– Andreas
Nov 21 '18 at 21:10







What SequenceInputStream? I see no SequenceInputStream in the code.

– Andreas
Nov 21 '18 at 21:10















What is completeIs? What is point of isList when you never use it?

– Andreas
Nov 21 '18 at 21:15





What is completeIs? What is point of isList when you never use it?

– Andreas
Nov 21 '18 at 21:15













@Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

– MrBuggy
Nov 21 '18 at 21:40





@Andreas Thank you for pointing out that detail (I've edited the code now). It's a copy paste error on my end.

– MrBuggy
Nov 21 '18 at 21:40












1 Answer
1






active

oldest

votes


















2














First thing: your way of converting a List into an Enumeration is bad (it copies the whole list into a new array that will then be wrapped in a Vector.)



It would be better to use



SequenceInputStream completeIs = new SequenceInputStream(Collections.enumeration(isList));


which wraps an Iterator in an Enumeration.





The problems:





  1. you expect completeIs.available() to return the total amount of available bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    [returns] an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream [..]





  2. you expect completeIs.read(readStr, 0, expectedNumBytesRead) to read all bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    The read method of SequenceInputStream tries to read the data from the current substream. If it fails to read any characters (because the substream has reached the end of the stream, it calls the close method of the current substream and) begins reading from the next substream.





To fully read data from an InputStream (be it a SequenceInputStream or some other variant), you always need to read in a loop until the read() method returns -1






share|improve this answer


























  • I edited the code in the question, I forgot to add that one line you mentioned

    – MrBuggy
    Nov 21 '18 at 21:45











  • @MrBuggy ah, I see it now. I've updated my answer accordingly

    – Thomas Kläger
    Nov 21 '18 at 21:53













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%2f53420309%2fsequenceinputstream-basic-test-failing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














First thing: your way of converting a List into an Enumeration is bad (it copies the whole list into a new array that will then be wrapped in a Vector.)



It would be better to use



SequenceInputStream completeIs = new SequenceInputStream(Collections.enumeration(isList));


which wraps an Iterator in an Enumeration.





The problems:





  1. you expect completeIs.available() to return the total amount of available bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    [returns] an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream [..]





  2. you expect completeIs.read(readStr, 0, expectedNumBytesRead) to read all bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    The read method of SequenceInputStream tries to read the data from the current substream. If it fails to read any characters (because the substream has reached the end of the stream, it calls the close method of the current substream and) begins reading from the next substream.





To fully read data from an InputStream (be it a SequenceInputStream or some other variant), you always need to read in a loop until the read() method returns -1






share|improve this answer


























  • I edited the code in the question, I forgot to add that one line you mentioned

    – MrBuggy
    Nov 21 '18 at 21:45











  • @MrBuggy ah, I see it now. I've updated my answer accordingly

    – Thomas Kläger
    Nov 21 '18 at 21:53


















2














First thing: your way of converting a List into an Enumeration is bad (it copies the whole list into a new array that will then be wrapped in a Vector.)



It would be better to use



SequenceInputStream completeIs = new SequenceInputStream(Collections.enumeration(isList));


which wraps an Iterator in an Enumeration.





The problems:





  1. you expect completeIs.available() to return the total amount of available bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    [returns] an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream [..]





  2. you expect completeIs.read(readStr, 0, expectedNumBytesRead) to read all bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    The read method of SequenceInputStream tries to read the data from the current substream. If it fails to read any characters (because the substream has reached the end of the stream, it calls the close method of the current substream and) begins reading from the next substream.





To fully read data from an InputStream (be it a SequenceInputStream or some other variant), you always need to read in a loop until the read() method returns -1






share|improve this answer


























  • I edited the code in the question, I forgot to add that one line you mentioned

    – MrBuggy
    Nov 21 '18 at 21:45











  • @MrBuggy ah, I see it now. I've updated my answer accordingly

    – Thomas Kläger
    Nov 21 '18 at 21:53
















2












2








2







First thing: your way of converting a List into an Enumeration is bad (it copies the whole list into a new array that will then be wrapped in a Vector.)



It would be better to use



SequenceInputStream completeIs = new SequenceInputStream(Collections.enumeration(isList));


which wraps an Iterator in an Enumeration.





The problems:





  1. you expect completeIs.available() to return the total amount of available bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    [returns] an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream [..]





  2. you expect completeIs.read(readStr, 0, expectedNumBytesRead) to read all bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    The read method of SequenceInputStream tries to read the data from the current substream. If it fails to read any characters (because the substream has reached the end of the stream, it calls the close method of the current substream and) begins reading from the next substream.





To fully read data from an InputStream (be it a SequenceInputStream or some other variant), you always need to read in a loop until the read() method returns -1






share|improve this answer















First thing: your way of converting a List into an Enumeration is bad (it copies the whole list into a new array that will then be wrapped in a Vector.)



It would be better to use



SequenceInputStream completeIs = new SequenceInputStream(Collections.enumeration(isList));


which wraps an Iterator in an Enumeration.





The problems:





  1. you expect completeIs.available() to return the total amount of available bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    [returns] an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream [..]





  2. you expect completeIs.read(readStr, 0, expectedNumBytesRead) to read all bytes from all enclosed InputStreams.



    However, according to the JavaDoc:




    The read method of SequenceInputStream tries to read the data from the current substream. If it fails to read any characters (because the substream has reached the end of the stream, it calls the close method of the current substream and) begins reading from the next substream.





To fully read data from an InputStream (be it a SequenceInputStream or some other variant), you always need to read in a loop until the read() method returns -1







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 22:08

























answered Nov 21 '18 at 21:42









Thomas KlägerThomas Kläger

7,0432819




7,0432819













  • I edited the code in the question, I forgot to add that one line you mentioned

    – MrBuggy
    Nov 21 '18 at 21:45











  • @MrBuggy ah, I see it now. I've updated my answer accordingly

    – Thomas Kläger
    Nov 21 '18 at 21:53





















  • I edited the code in the question, I forgot to add that one line you mentioned

    – MrBuggy
    Nov 21 '18 at 21:45











  • @MrBuggy ah, I see it now. I've updated my answer accordingly

    – Thomas Kläger
    Nov 21 '18 at 21:53



















I edited the code in the question, I forgot to add that one line you mentioned

– MrBuggy
Nov 21 '18 at 21:45





I edited the code in the question, I forgot to add that one line you mentioned

– MrBuggy
Nov 21 '18 at 21:45













@MrBuggy ah, I see it now. I've updated my answer accordingly

– Thomas Kläger
Nov 21 '18 at 21:53







@MrBuggy ah, I see it now. I've updated my answer accordingly

– Thomas Kläger
Nov 21 '18 at 21:53






















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%2f53420309%2fsequenceinputstream-basic-test-failing%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents