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.

HDGFLZW.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hdgf;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import org.apache.poi.util.LZWDecompresser;
  21. /**
  22. * A decoder for the crazy LZW implementation used
  23. * in Visio.
  24. * According to VSDump, "it's a slightly perverted version of LZW
  25. * compression, with inverted meaning of flag byte and 0xFEE as an
  26. * 'initial shift'". It uses 12 bit codes
  27. * (http://www.gnome.ru/projects/vsdump_en.html)
  28. *
  29. * Two good resources on LZW are:
  30. * http://en.wikipedia.org/wiki/LZW
  31. * http://marknelson.us/1989/10/01/lzw-data-compression/
  32. */
  33. public class HDGFLZW extends LZWDecompresser {
  34. public HDGFLZW() {
  35. // Out flag is the wrong way round!
  36. // Length wise, we're 3 longer than we say, so the max len is 19
  37. // Endian wise, we're little endian, so 0x1234 is pos 0x312
  38. super(false, 3, false);
  39. }
  40. /**
  41. * Compress the given input stream, returning the array of bytes
  42. * of the compressed input
  43. *
  44. * @param src the compression source byte
  45. * @return the compressed stream as bytes
  46. *
  47. * @throws IOException when the InputStream can't be read
  48. */
  49. public byte[] compress(InputStream src) throws IOException {
  50. ByteArrayOutputStream res = new ByteArrayOutputStream();
  51. compress(src,res);
  52. return res.toByteArray();
  53. }
  54. /**
  55. * We have a slight shift by 18 bytes
  56. */
  57. @Override
  58. protected int adjustDictionaryOffset(int pntr) {
  59. if(pntr > 4078) {
  60. pntr = pntr - 4078;
  61. } else {
  62. pntr = pntr + 18;
  63. }
  64. return pntr;
  65. }
  66. /**
  67. * We want an empty dictionary, so do nothing
  68. */
  69. @Override
  70. protected int populateDictionary(byte[] dict) {
  71. return 0;
  72. }
  73. /**
  74. * Performs the Visio compatible streaming LZW compression.
  75. *
  76. * @param src the input bytes for the compression
  77. * @param res the OutputStream which receives the compressed bytes
  78. *
  79. * @throws IOException when the InputStream can't be read
  80. * or the OutputStream can't be written to
  81. */
  82. public void compress(InputStream src, OutputStream res) throws IOException {
  83. HDGFLZWCompressor c = new HDGFLZWCompressor(res);
  84. c.compress(src);
  85. }
  86. }