Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for not closing the output stream opened by user #941

Merged
merged 9 commits into from
May 24, 2021

Conversation

Kerwinooooo
Copy link
Contributor

Thanks for contributing.

Description

Add a boolean attribute autoclose in WriteOptions and Builder, whose default value is true. When Builder is constructed with OutputStream or Writer, set autoclose as false.

Corresponding issue is #750

Testing

Yes. 2 unit tests for testing whether FileOutputStream and OutputStreamWriter close.

@@ -61,7 +61,7 @@ public void write(Table table, CsvWriteOptions options) {
} finally {
if (csvWriter != null) {
csvWriter.flush();
csvWriter.close();
if (options.autoClose()) csvWriter.close();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FixedWidthWriter would need to be updated as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FixedWidthWriter would need to be updated as well

Fixed. But I have some problem with FixedWidthWriter when I try to output the table.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
FixedWidthWriteOptions options =
    new FixedWidthWriteOptions.Builder(baos)
        .header(new FixedWidthFields(10,10))
        .build();
FixedWidthWriter writer = new FixedWidthWriter();
writer.write(table, options);

The last line of code got IllegalStateException. When FixedWidthWriterSettings is constructed with FixedWidthWriteOptions, fieldLengths is neved assigned. I think it will be fixed in another issue.

@lwhite1
Copy link
Collaborator

lwhite1 commented May 22, 2021

Does this need to be an option or could the output stream always be left open for writer and stream, but closed for everything else?

@ChangzhenZhang
Copy link
Contributor

the output stream always be left open for writer and stream, but closed for everything else

I think the output stream should always be left open for writer and stream, but closed for others.
Do you mean that this change should be put in other places, not in WriteOptions?

@lwhite1
Copy link
Collaborator

lwhite1 commented May 22, 2021 via email

@ChangzhenZhang
Copy link
Contributor

ok, I will change it later.

@ChangzhenZhang
Copy link
Contributor

@lwhite1, I reviewed the code, and I found that I put the boolean value in WriteOptions only because it can be distinguished here according to the different builders. Is there any other place can distinguish the builder with different output stream? I will try to put this value there later.

@lwhite1
Copy link
Collaborator

lwhite1 commented May 23, 2021 via email

@ChangzhenZhang
Copy link
Contributor

i don't know, actually. I recall from the last time i looked at it that it was difficult to find the right place to close the streams, so I'm not surprised. You can leave it in WriteOptions. Just document in the code why it's there, and that is not exposed as an actual option. (BTW, for the documentation I assume you meant "distribute" rather than "distinguish") thanks

On Sun, May 23, 2021 at 2:51 AM Changzhen Zhang @.***> wrote: @lwhite1 https://github.com/lwhite1, I reviewed the code, and I found that I put the boolean value in WriteOptions only because it can be distinguished here according to the different builders. Is there any other place can distinguish the builder with different output stream? I will try to put this value there later. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#941 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2FPAQXYSVC4QZR3QASYWLTPCQY3ANCNFSM45GVOVNQ .

Please check if the document is appropriate. Thanks!

@lwhite1 lwhite1 merged commit 8abf422 into jtablesaw:master May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants