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.

CompressingDataOutputStream.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* *******************************************************************
  2. * Copyright (c) 2010 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement (SpringSource)
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. /**
  18. * A variation of a DataOutputStream that is linked to a constant pool writer. The linked constant pool can be used to compress
  19. * objects into to simple index references into the constant pool. The corresponding decompression is done in the
  20. * VersionedDataInputStream.
  21. *
  22. * @author Andy Clement
  23. */
  24. public class CompressingDataOutputStream extends DataOutputStream {
  25. private ConstantPoolWriter constantPoolWriter;
  26. public boolean compressionEnabled = true;
  27. public CompressingDataOutputStream(ByteArrayOutputStream baos, ConstantPoolWriter constantPoolWriter) {
  28. super(baos);
  29. this.constantPoolWriter = constantPoolWriter;
  30. }
  31. public CompressingDataOutputStream(FileOutputStream fos) {
  32. super(fos);
  33. }
  34. public boolean canCompress() {
  35. return constantPoolWriter != null && compressionEnabled;
  36. }
  37. /**
  38. * @param signature of the form 'La/b/c/d;'
  39. * @return the constant pool index
  40. */
  41. public int compressSignature(String signature) {
  42. if (constantPoolWriter == null) {
  43. throw new IllegalStateException();
  44. }
  45. return constantPoolWriter.writeUtf8(signature);
  46. }
  47. /**
  48. * @param filepath a file system path 'c:\a\b\c.txt' or '/a/b/c.txt'
  49. * @return the constant pool index
  50. */
  51. public int compressFilepath(String filepath) {
  52. if (constantPoolWriter == null) {
  53. throw new IllegalStateException();
  54. }
  55. return constantPoolWriter.writeUtf8(filepath);
  56. }
  57. /**
  58. * @param name a simple name (for example a method or field name)
  59. * @return the constant pool index
  60. */
  61. public int compressName(String name) {
  62. if (constantPoolWriter == null) {
  63. throw new IllegalStateException();
  64. }
  65. return constantPoolWriter.writeUtf8(name);
  66. }
  67. /**
  68. * @param name a simple name (for example a method or field name)
  69. */
  70. public void writeCompressedName(String name) throws IOException {
  71. writeShort(compressName(name));
  72. }
  73. /**
  74. * @param signature of the form 'La/b/c/d;'
  75. */
  76. public void writeCompressedSignature(String signature) throws IOException {
  77. writeShort(compressSignature(signature));
  78. }
  79. /**
  80. * @param path a file system path 'c:\a\b\c.txt' or '/a/b/c.txt'
  81. */
  82. public void writeCompressedPath(String path) throws IOException {
  83. writeShort(compressFilepath(path));
  84. }
  85. }