Re: [ceylon-dev] Java byte and Java interop

105 views
Skip to first unread message

Tako Schotanus

unread,
Jan 21, 2013, 7:21:04 AM1/21/13
to ceylon-dev
Hi Anton,

yes, I think you've hit on a bug, because the compiler shouldn't allow you to do this (Ceylon arrays created in the standard way are not byte arrays so can't be used in this case).

Would you be so kind to open up a ticket for it here: https://github.com/ceylon/ceylon-compiler/issues ?

To make this work you can use the toplevel createByteArray() function from the ceylon.interop.java module that's available on The Herd (just add import ceylon.interop.java '0.4.1'; to your module.ceylon file).

Hope this helps,
-Tako




-Tako


On Mon, Jan 21, 2013 at 12:53 PM, Anton Arhipov <ant.a...@gmail.com> wrote:

I'd like to translate the following pice of code from Java to Ceylon and while it compiles just fine, it throws a ClassCastException at runtime.

Java:

byte[] fileData = new byte[fileLength];

FileInputStream fileIn = new FileInputStream(file);

fileIn.read(fileData);


Ceylon:

Array<Integer> fileData = arrayOfSize<Integer>(fileLength, 0);

value fileIn = FileInputStream(file);

fileIn.read(fileData);


Ceylon version results in the following exception at runtime:

Exception in thread "Thread-0" java.lang.ClassCastException: [J cannot be cast to [B

     at com.zt.HTTPServer.run(classes.ceylon:59)

     at java.lang.Thread.run(Thread.java:722)


Is there a trick that I've missed or is it a bug that I've hit?


Thanks, 

Anton





--
You received this message because you are subscribed to the Google Groups "ceylon-dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ceylon-dev/-/MxPGLUTEWrYJ.
To post to this group, send email to ceylo...@googlegroups.com.
To unsubscribe from this group, send email to ceylon-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ceylon-dev?hl=en.

Stephane Epardaud

unread,
Jan 21, 2013, 7:22:25 AM1/21/13
to ceylon-dev
Hello,

arrayOfSize<Integer> will create an array of Integer, not byte, so you need the  createByteArray() method from the ceylon.interop.java module (on Herd) to create byte arrays, as Tako said.

Now, this is certainly a bug that the compiler should have caught and stopped you from trying, so could you please report this to https://github.com/ceylon/ceylon-compiler/issues/new ?

On the other hand, if you want to stay within Ceylon-land and try the ceylon.io module from our SDK (also on Herd) you can try:

Path path = parsePath("foo.txt");

OpenFile file = newOpenFile(path.resource);
Decoder decoder = utf8.newDecoder();
file.readFully((ByteBuffer buffer) => decoder.decode(buffer));
file.close();

String contents = decoder.done();

It's a bit long ATM, but we will produce much shorter utilities code in the near future so it can only improve.

Hope that helps. Let us know if you have any other issue.


On 21 January 2013 12:53, Anton Arhipov <ant.a...@gmail.com> wrote:

I'd like to translate the following pice of code from Java to Ceylon and while it compiles just fine, it throws a ClassCastException at runtime.

Java:

byte[] fileData = new byte[fileLength];

FileInputStream fileIn = new FileInputStream(file);

fileIn.read(fileData);


Ceylon:

Array<Integer> fileData = arrayOfSize<Integer>(fileLength, 0);

value fileIn = FileInputStream(file);

fileIn.read(fileData);


Ceylon version results in the following exception at runtime:

Exception in thread "Thread-0" java.lang.ClassCastException: [J cannot be cast to [B

     at com.zt.HTTPServer.run(classes.ceylon:59)

     at java.lang.Thread.run(Thread.java:722)


Is there a trick that I've missed or is it a bug that I've hit?


Thanks, 

Anton





--
You received this message because you are subscribed to the Google Groups "ceylon-dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ceylon-dev/-/MxPGLUTEWrYJ.
To post to this group, send email to ceylo...@googlegroups.com.
To unsubscribe from this group, send email to ceylon-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ceylon-dev?hl=en.



--
Stéphane Épardaud

Anton Arhipov

unread,
Jan 29, 2013, 7:34:05 AM1/29/13
to ceylo...@googlegroups.com

Indeed, the introp module solved the issue for me.

I'll give ceylon-io a try. 

Thanks!

Gavin King

unread,
Jan 29, 2013, 7:55:44 AM1/29/13
to ceylo...@googlegroups.com
Thanks, Anton, FTR I think your code is perfectly reasonable and we should try to make it work if we can. The problem is that we definitely can't make it work in full generality without a dedicated native Byte class, which I would really like to avoid introducing if possible.

Sent from my iPhone
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-dev+...@googlegroups.com.

To post to this group, send email to ceylo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages