Results 1 to 9 of 9

Thread: InputStream/Reader cross operation

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    InputStream/Reader cross operation

    I want to decode a Base64 encoded file. I did a Google search and the easiest-to-use library seemed to be two Java classes, so I decided to do my app in Java.

    I want to read a Outlook Express 4 mail file, where parts are encoded and others are not. I'm currently reading in the whole file, converting it to a string and searching for encoded blocks. Works great, finds the blocks correctly.
    Now my problem: I end up with a String object named base64 which contains the encoded string. The decoding class is derived from FilterInputStream and expects an InputStream object passed to the constructor. But the only InputStream I can see that handles strings is StringInputString which is deprecated and saim the String directlyd to behave incorrectly. You should obviously use StringReader to read from strings.
    My question: how can I get some kind of InputStream object, either fro or from StringReader?

    For the time being I'll convert the String to a byte array and use a ByteArrayInputStream, but this is notoriously inefficient!

    Thx in advance for any help.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Works now, but only with the byte array trick. I still appreciate any help.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Have you tried StringBufferInputStream?

    Have a look at http://java.sun.com/j2se/1.4/docs/api/ then in java.io

    Hopefully I understand your question, but it's unlikely

    HD

  4. #4

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It says StringBufferInputStream is deprecated because it won't convert characters to bytes correctly.

    And the reference is permanently open...

    Here's my code so far. It works for encoded text, but not binary data

    This file also contains the jar file of the Base64 decoding class.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Here's a part of the sample file I use as source.

    You need to pass the name of the file on the command line.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Unfortunately I can't help at the moment - if no-one replies before tomorrow I should be able to while I'm supposed to be working . My home PC is VERY bare at the moment after I wiped it - so I can't test anything.

    If you still need help tomorrow I'd be glad to give you a hand.

    Sorry

    HD

  7. #7

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I managed to get it to work, but it involves several unnecessary copies, so I'd be happy if someone would find a way that uses less copies.
    Thanks.
    Here's the final code.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    I've had a look through the java docs and through some of my own work. The only real way to do this is to use Byte Arrays.

    The problem with the StringBufferInputStream is that it only uses the low 8-bits of the String. Therefore if you know your encoding its not a problem (8-bit obviously).

    From all other examples I (and others) have used the Byte approach. The only advice is that you don't do the copying explicitly, just 'shove' it in the constructor for the Base64InputStream:

    String base64 = str.substring(index, end);
    Base64InputStream b64is = new Base64InputStream(new ByteArrayInputStream(base64.getBytes()));

    rather than

    String base64 = str.substring(index, end);
    byte[] bar = base64.getBytes();
    ByteArrayInputStream bais = new ByteArrayInputStream(bar);
    Base64InputStream b64is = new Base64InputStream(new ByteArrayInputStrbais);

    Again, if you know that the encoding is OK, then StringBufferInputStream is fine, but otherwise this is the generally accepted approach.

    HD

  9. #9

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Thanks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width