Bug with Base 64 coding in TBase64Utils
---------------------------------------

                 Key: THRIFT-1156
                 URL: https://issues.apache.org/jira/browse/THRIFT-1156
             Project: Thrift
          Issue Type: Bug
          Components: Java - Library
    Affects Versions: 0.6
         Environment: Java SE, Android
            Reporter: Głowka Rafał


There are some problems with coding byte[] when values are less that 0.

The example code :
        public static void main(String[] args) 
        {
                byte[] data = new byte[256];
                byte[] data2 = new byte[512];
                for (int i = 0; i < 512; i++)
                        data2[i] = 0;
                for (byte b = -128; b < 127; b++)
                        data[b + 128] = b;

                int len = 256;
                int off = 0;
                int off2 = 0;
                while (len >= 3) {
                        // Encode 3 bytes at a time
                        TBase64Utils.encode(data, off, 3, data2, off2);
                        off += 3;
                        off2 += 4;
                        len -= 3;
                }
                if (len > 0) {
                        // Encode remainder
                        TBase64Utils.encode(data, off, len, data2, off2);
                }

                len = 256;
                off = 0;
                off2 = 0;
                while (len >= 3) {
                        // Encode 3 bytes at a time
                        TBase64Utils.decode(data2, off2, 4, data, off);
                        off += 3;
                        len -= 3;
                        off2 += 4;
                }
                if (len > 0) {
                        // Encode remainder
                        TBase64Utils.decode(data2, off2, len, data, off);
                }

                for (byte b = -128; b < 127; b++)
                        System.out.println("should be : " + (b) + " is : " + 
data[b + 128]);
        }


The results : 
...
should be : -18 is : -18
should be : -17 is : -18
should be : -16 is : -1
should be : -15 is : -15
should be : -14 is : -15
should be : -13 is : -14
should be : -12 is : -12
should be : -11 is : -12
should be : -10 is : -11
should be : -9 is : -9
should be : -8 is : -5
should be : -7 is : -8
should be : -6 is : -6
should be : -5 is : -6
should be : -4 is : -5
should be : -3 is : -3
should be : -2 is : -3
should be : -1 is : -1
should be : 0 is : 0
should be : 1 is : 1
...



--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to