JRuby iText question regarding strings and bytearrays

Hello everyone

I am in a situation where I have some binary data for a PDF file in a
simple ruby string.

I would like to use iText to do some work on the PDF represented by the
binary data in this string.

I have two options.

  1. Write the PDF to the filesystem then read it using iText.
  2. read from the ruby string directly.

Now, writing to the filesystem just to read it once 50 milliseconds
later offends my sensibilities :wink: so I am having a look at option 2 but
am having…fun. (Note, I haven’t used Java in anger since university
days a decade ago)

Basically you can create a PdfReader instance using the following java
constructor.

publicPdfReader(InputStream
http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html is)

So far so good, all I need to create an InputStream from a ruby string,
how hard can that be?

Well it turns out, not obvious. After finding out that the
StringBufferInputStream class has been deprecated with no real useful
replacement
(http://www.velocityreviews.com/forums/t367242-what-replaces-stringbufferinputstream.html)
I find out that if I want to get an InputStream that iText recognises
then I have to use the following Java code:

InputStream in = new ByteArrayInputStream(someString.getBytes(encoding)
);

Unfortunately i have no idea how to convert a ruby string into the
ByteArray format required, can anyone give a hint? (Also, what if you do
not know the encoding of the PDF?!).

There is another constructor that allows you to use

publicPdfReader(byte[] pdfIn)

But from my reading of that I still need to do the conversion.

So having investigated all that should I just bite the bullet, write the
PDF to the filesystem and read it using iText? Not the cleanest solution
but I think it might be more stable in the long-run if dealing with
various encodings etc. Or if there is another option that I have not
considered due to my supremely rusty java skills then I would love to
hear it.

Cheers

Jeff

P.S. How on earth do Java guys put up with API changes like that without
wanting to murder the people in charge of the language spec? :stuck_out_tongue:

Hello all

5 seconds after writing that email I thought about a different tack on
searching and tried a simple “ruby string java bytearray” and found the
to_java_bytes and from_java_bytes methods on the String class which
solves my problem nicely.

Easy when you think of the correct search terms…

Sorry for the noise

Jeff