From e6e8945040db1c9bcff0aea35775190c3944b718 Mon Sep 17 00:00:00 2001 From: Kelly Campbell Date: Thu, 1 Feb 2001 09:08:08 +0000 Subject: [PATCH] Fixed an off by one error in the end of stream encoding part of ASCII85Filter as reported by Alex Cherepanov. Cleaned up the code a bit and added some better comments too. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194006 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/pdf/ASCII85Filter.java | 137 +++++++++++++--------- 1 file changed, 82 insertions(+), 55 deletions(-) diff --git a/src/org/apache/fop/pdf/ASCII85Filter.java b/src/org/apache/fop/pdf/ASCII85Filter.java index 13368c4ee..7ef711c45 100644 --- a/src/org/apache/fop/pdf/ASCII85Filter.java +++ b/src/org/apache/fop/pdf/ASCII85Filter.java @@ -79,105 +79,132 @@ public class ASCII85Filter extends PDFFilter public byte[] encode(byte[] data) { - StringBuffer buffer = new StringBuffer(); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int i; int total = 0; int diff = 0; + // first encode the majority of the data + // each 4 byte group becomes a 5 byte group for (i = 0; i+3 < data.length; i+=4) { - byte b1 = data[i]; - byte b2 = data[i+1]; - byte b3 = data[i+2]; - byte b4 = data[i+3]; - - long val = ((b1 << 24) & 0xff000000L) - + ((b2 << 16) & 0xff0000L) - + ((b3 << 8) & 0xff00L) - + (b4 & 0xffL); - String conv = convertLong(val); - buffer.append(conv); - + long val = ((data[i] << 24) & 0xff000000L) // note: must have the L at the + + ((data[i+1] << 16) & 0xff0000L) // end, otherwise you get into + + ((data[i+2] << 8) & 0xff00L) // weird signed value problems + + (data[i+3] & 0xffL); // cause we're using a full 32 bits + byte[] conv = convertWord(val); + + buffer.write(conv,0,conv.length); } - + // now take care of the trailing few bytes. + // with n leftover bytes, we append 0 bytes to make a full group of 4 + // then convert like normal (except not applying the special zero rule) + // and write out the first n+1 bytes from the result if (i < data.length) { int n = data.length - i; - byte b1,b2,b3,b4; - b1 = data[i++]; - if (i < data.length) { - b2 = data[i++]; - } - else { - b2 = 0; - } - if (i < data.length) { - b3 = data[i++]; - } - else { - b3 = 0; + byte[] lastdata = new byte[4]; + for (int j=0; j<4; j++) { + if (j