Patrick LeBoutillier wrote: >Suppose you call a Java method (from Perl) that returns a String. What >does Inline::Java do with the String? It nicely converts it to a Perl >scalar for you and allows you to forget all about the String object. >Unfortunately that exactly what is happening here also :( > > >But you can easily work around this by creating a wrapper method like such: > > >use strict; >use warnings; >use Inline ( > Java => 'DATA', > ) ; > >foreach my $b (@{StringOps->getBytes("Test Me")}){ > print "$b\n" ; >} > > >__DATA__ >__Java__ > > >class StringOps { > static public byte [] getBytes(String s){ > return s.getBytes() ; > } >} > > >
Once again a corner case rears its head. The problem with this wrapper method of getting byte arrays is that java encodes strings. If there's binary data in a scalar, that encoding means what you get back might not be what you sent in. So, what I'm doing now is using a ByteArrayOutputStream to accumulate bytes. Or I'm trying to. I still haven't quite figured out how to pass the write method an argument it understands. Once that's working I can call toByteArray and pass that in unmodified. I'm just vague on how to exactly get this to work. I can't coerce it (or I'd just create the byte array myself). More and more its looking like handling binary streams is going to be a matter of base64 encoding everything. Please tell me I'm overlooking something simple. I was hoping something as simple as my $stream = $self->class_name('java.io.ByteArrayOutputStream')->new(scalar @bytes); $stream->write(Inline::Java::coerce('[B',[EMAIL PROTECTED],"[B;"),0,length @bytes); would work. -- J.