Jason,

Have you tried something like this:

################
use strict ;
use warnings ;
use Inline (
    Java => 'DATA',
    STUDY => ['java.io.ByteArrayOutputStream'],
) ;

my $os = new java::io::ByteArrayOutputStream() ;
my $scalar = "test\n" ;
print $scalar ;
my @bytes = map {ord($_)} split('', $scalar) ;
print join('.', @bytes) . "\n" ;

$os->write([EMAIL PROTECTED], 0, scalar(@bytes)) ;
my @java_bytes = @{$os->toByteArray()} ;
print join('.', @java_bytes) . "\n" ;
print join('', map{chr($_)} @java_bytes) ;
#############

Is that what you are looking for? It may be a bit expensive to split
the actual scalars into lists of byte values, but I'm pretty sure this
technique will leave them unchanged.

Patrick


On 9/22/05, Jason Stelzer <[EMAIL PROTECTED]> wrote:
> 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.
>


--
=====================
Patrick LeBoutillier
Laval, Québec, Canada

Reply via email to