Thanks to Bindul and Luc.
You both hit my problem on the head. Change from a Writer to an
OutputStream did the trick.
I appreciate your help.
Chris...
Bindul Bhowmik wrote:
On 8/19/06, Chris Hane <[EMAIL PROTECTED]> wrote:
Ok - I agree that this is strange. However, when I tried just bytes,
the output file was even worse. So that tells me I don't know much
about how to correctly do IO (which I don't). Can you suggest an
alternative so that the bytes are outputted correctly.
I have tried a method like the following; but the output file isn't
correct either. The straight bytes out produces even more incorrectly
translated characters when looking at the raw file using wordpad. I
have lot more ? characters than using the one that converted to a String
first. Could I be reading the input file incorrectly in the
getBytesFromFile method.
private static void test3() throws IOException{
byte[] inBytes = getBytesFromFile(new File("c:\\var\\pdfif.txt"));
Base64 decoder = new Base64();
byte[] decodedBytes= decoder.decode(inBytes);
BufferedWriter out = new BufferedWriter(new FileWriter(new
File("c:\\var\\test.pdf")));
for(int x=0; x<decodedBytes.length; x++){
out.write(decodedBytes[x]);
}
out.flush();
out.close();
}
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >=
0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file
"+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
Thanks for looking at this.
Chris....
[EMAIL PROTECTED] wrote:
> Selon Chris Hane <[EMAIL PROTECTED]>:
>
>
>
>> char[] msgChars = new String(decoder.decode(b)).toCharArray();
>>
>
> This looks rather strange to me. When you decode something from
Base64, you
> expect the output to be (or contain in your case) pure binary data,
you cannot
> put it into a character array. When you do this, the JVM uses its
default
> character encoding setting and may well mangle your data. You
should really
> stick to bytes, not characters.
>
> I'm not sure the problem is really here, though. It just looks
strange to me.
>
> Luc
>
>
Hello Chris,
You might want to change from BufferedWriter and FileWriter to
BufferedOutputStream and FileOutputStream. That is because writers use
characters for processing the data.
Regards,
Bindul
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]