You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ASCII85InputStreamTestCase.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.util;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.commons.io.IOUtils;
  22. import org.apache.commons.io.output.ByteArrayOutputStream;
  23. import org.apache.fop.pdf.PDFText;
  24. import junit.framework.TestCase;
  25. /**
  26. * Test case for ASCII85InputStream.
  27. * <p>
  28. * ATTENTION: Some of the tests here depend on the correct behaviour of
  29. * ASCII85OutputStream. If something fails here make sure
  30. * ASCII85OutputStreamTestCase runs!
  31. */
  32. public class ASCII85InputStreamTestCase extends TestCase {
  33. private static final boolean DEBUG = false;
  34. /**
  35. * @see junit.framework.TestCase#TestCase(String)
  36. */
  37. public ASCII85InputStreamTestCase(String name) {
  38. super(name);
  39. }
  40. private byte[] decode(String text) throws Exception {
  41. byte[] ascii85 = text.getBytes("US-ASCII");
  42. InputStream in = new ByteArrayInputStream(ascii85);
  43. InputStream decoder = new ASCII85InputStream(in);
  44. return IOUtils.toByteArray(decoder);
  45. }
  46. private byte[] getChunk(int count) {
  47. byte[] buf = new byte[count];
  48. System.arraycopy(ASCII85OutputStreamTestCase.DATA, 0, buf, 0, buf.length);
  49. return buf;
  50. }
  51. private String encode(byte[] data, int len) throws Exception {
  52. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  53. java.io.OutputStream out = new ASCII85OutputStream(baout);
  54. out.write(data, 0, len);
  55. out.close();
  56. return new String(baout.toByteArray(), "US-ASCII");
  57. }
  58. private void innerTestDecode(byte[] data) throws Exception {
  59. String encoded = encode(data, data.length);
  60. if (DEBUG) {
  61. if (data[0] == 0) {
  62. System.out.println("self-encode: " + data.length + " chunk 000102030405...");
  63. } else {
  64. System.out.println("self-encode: " + new String(data, "US-ASCII")
  65. + " " + PDFText.toHex(data));
  66. }
  67. System.out.println(" ---> " + encoded);
  68. }
  69. byte[] decoded = decode(encoded);
  70. if (DEBUG) {
  71. if (data[0] == 0) {
  72. System.out.println("decoded: " + data.length + " chunk 000102030405...");
  73. } else {
  74. System.out.println("decoded: " + new String(decoded, "US-ASCII")
  75. + " " + PDFText.toHex(decoded));
  76. }
  77. }
  78. assertEquals(PDFText.toHex(data), PDFText.toHex(decoded));
  79. }
  80. /**
  81. * Tests the output of ASCII85.
  82. * @throws Exception if an error occurs
  83. */
  84. public void testDecode() throws Exception {
  85. byte[] buf;
  86. innerTestDecode("1. Bodypart".getBytes("US-ASCII"));
  87. if (DEBUG) {
  88. System.out.println("===========================================");
  89. }
  90. innerTestDecode(getChunk(1));
  91. innerTestDecode(getChunk(2));
  92. innerTestDecode(getChunk(3));
  93. innerTestDecode(getChunk(4));
  94. innerTestDecode(getChunk(5));
  95. if (DEBUG) {
  96. System.out.println("===========================================");
  97. }
  98. innerTestDecode(getChunk(10));
  99. innerTestDecode(getChunk(62));
  100. innerTestDecode(getChunk(63));
  101. innerTestDecode(getChunk(64));
  102. innerTestDecode(getChunk(65));
  103. if (DEBUG) {
  104. System.out.println("===========================================");
  105. }
  106. String sz;
  107. sz = PDFText.toHex(decode("zz~>"));
  108. assertEquals(PDFText.toHex(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}), sz);
  109. sz = PDFText.toHex(decode("z\t \0z\n~>"));
  110. assertEquals(PDFText.toHex(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}), sz);
  111. if (DEBUG) {
  112. System.out.println("===========================================");
  113. }
  114. try {
  115. decode("vz~>");
  116. fail("Illegal character should be detected");
  117. } catch (IOException ioe) {
  118. //expected
  119. }
  120. /* DISABLED because of try/catch in InputStream.read(byte[], int, int).
  121. * Only the exception happening on the first byte in a block is being
  122. * reported. BUG in JDK???
  123. *
  124. try {
  125. decode("zv~>");
  126. fail("Illegal character should be detected");
  127. } catch (IOException ioe) {
  128. //expected
  129. }*/
  130. }
  131. private byte[] getFullASCIIRange() {
  132. java.io.ByteArrayOutputStream baout = new java.io.ByteArrayOutputStream(256);
  133. for (int i = 254; i < 256; i++) {
  134. baout.write(i);
  135. }
  136. return baout.toByteArray();
  137. }
  138. /**
  139. * Tests the full 8-bit ASCII range.
  140. * @throws Exception if an error occurs
  141. */
  142. public void testFullASCIIRange() throws Exception {
  143. innerTestDecode(getFullASCIIRange());
  144. }
  145. }