Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Add Java Input/Output Stream wrappers to ceylon.interop.java #687

Open
qdzo opened this issue Sep 14, 2017 · 1 comment
Open

Add Java Input/Output Stream wrappers to ceylon.interop.java #687

qdzo opened this issue Sep 14, 2017 · 1 comment

Comments

@qdzo
Copy link

qdzo commented Sep 14, 2017

There are sometimes need to use Java's InputStream and OutputStream, and it will be good to have some ceylon's wrappers on them.

There are similar interfaces in SDK :

  • ceylon.buffer.readers.Reader
  • ceylon.io.WritableFileDescriptor
  • ceylon.io.ReadableFileDescriptor

and there are some suitable candidates for java-to-ceylon way.

  • ceylon.io.impl.InputStreamAdapter
  • ceylon.io.impl.OutputStreamAdapter

But need an opposite way ceylon->java.

Something like that

class CeylonInputStream(ByteBuffer buffer) extends InputStream() {
    assert (is JavaByteBuffer implementation = buffer.implementation);
    shared actual Integer read() {
        try {
            return implementation.get().unsigned;
        }
        catch (BufferUnderflowException e) {
            return -1;
        }
    }
}
class CeylonOutputStream(ByteBuffer buffer) extends OutputStream() {
    assert (is JavaByteBuffer implementation = buffer.implementation);
    shared actual void write(Integer b) {
        try {
            implementation.put(b.byte);
        }
        catch (BufferOverflowException e){
        }
    }
}

What do you think about this?

@qdzo
Copy link
Author

qdzo commented Sep 14, 2017

Or simpler

class CeylonInputStream(ByteBuffer buffer) extends InputStream() {
    
    shared actual Integer read() {
        try {
            return buffer.get().unsigned;
        }
        catch (BufferUnderflowException e) {
            return -1;
        }
    }
}
class CeylonOutputStream(ByteBuffer buffer) extends OutputStream() {

   shared actual void write(Integer b) {
        try {
            buffer.put(b.byte);
        }
        catch (BufferOverflowException e){
        }
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant