I/O confusion

59 views
Skip to first unread message

Dirk Lattermann

unread,
Jun 20, 2016, 5:14:29 PM6/20/16
to ceylon...@googlegroups.com
Is there an API to read characters from a file or must I read into a
ByteBuffer, using a Reader, and then transform with a Charset to a
String (or CharacterBuffer)?

If the latter, how do I treat read bytes at the end of the ByteBuffer
that are not completely used for a multibyte character? The
documentation just says Charset.decode decodes all of input, returning
all of the output, but what happens if the last part of input needs the
first part of the next input to produce an output?

I'm also a bit confused about the Reader in ceylon.buffers, the
ReadableFileDescriptor and FileDescriptorReader in ceylon.io and the
Reader in ceylon.file. What is the preferred way to implement something
envolving reading of resources to have it the most versatile way so
that it can read files, sockets, anything?

And is there really no file or other i/o for javascript? Maybe I just
don't know where to start looking.

Thanks, Dirk.

Stephane Epardaud

unread,
Jun 21, 2016, 5:23:36 AM6/21/16
to ceylon...@googlegroups.com
Hi,

I agree that the API documentation is outdated and confusing and we're lacking shortcut methods. The buffer API was updated but the docs were not.

Here's how you'd read a file fully:

  OpenFile file = newOpenFile(path.resource);
  value decoder = utf8.cumulativeDecoder();
  file.readFully((ByteBuffer buffer) => decoder.more(buffer));
  decoder.done();
  file.close();

As for the multibyte remains, the decoder will actually read the leading parts and keep them for the next iteration. If there are remaining parts when you call `decoder.done()` it will throw.

As for `Reader` in `ceylon.file` they can only read using the default platform encoding.

I'm going to open issues about those topics.

HTH?



--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/20160620231427.72fb291d%40dinu.
For more options, visit https://groups.google.com/d/optout.



--
Stéphane Épardaud

Gavin King

unread,
Jun 21, 2016, 10:39:16 AM6/21/16
to ceylon...@googlegroups.com

Dirk Lattermann

unread,
Jun 21, 2016, 12:16:03 PM6/21/16
to ceylon...@googlegroups.com
Hello Stéphane,

thank you, that already helps much.

There are some questions open, however.
If I want/must read the input piecewise, how can I get at the
intermediate characters that are decoded so far?

Is there some common "Reader" that can read from a File, a URL (socket),
a ZIP ...? Something like the abstract InputStream from Java which can
be wrapped to other input streams for special behaviour, like pushing
back read bytes to read them again, etc. Do you already have a design
for that?

Thanks again, Dirk

Am Tue, 21 Jun 2016 11:23:35 +0200
schrieb Stephane Epardaud <stephane...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages