aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/CompressingDataOutputStream.java
blob: 2100775759333ab3bd4c8f1b6818bab69f6bf7bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* *******************************************************************
 * Copyright (c) 2010 Contributors
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 * Andy Clement (SpringSource)
 * ******************************************************************/
package org.aspectj.weaver;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * A variation of a DataOutputStream that is linked to a constant pool writer. The linked constant pool can be used to compress
 * objects into to simple index references into the constant pool. The corresponding decompression is done in the
 * VersionedDataInputStream.
 *
 * @author Andy Clement
 */
public class CompressingDataOutputStream extends DataOutputStream {

	private ConstantPoolWriter constantPoolWriter;
	public boolean compressionEnabled = true;

	public CompressingDataOutputStream(ByteArrayOutputStream baos, ConstantPoolWriter constantPoolWriter) {
		super(baos);
		this.constantPoolWriter = constantPoolWriter;
	}

	public CompressingDataOutputStream(FileOutputStream fos) {
		super(fos);
	}

	public boolean canCompress() {
		return constantPoolWriter != null && compressionEnabled;
	}

	/**
	 * @param signature of the form 'La/b/c/d;'
	 * @return the constant pool index
	 */
	public int compressSignature(String signature) {
		if (constantPoolWriter == null) {
			throw new IllegalStateException();
		}
		return constantPoolWriter.writeUtf8(signature);
	}

	/**
	 * @param filepath a file system path 'c:\a\b\c.txt' or '/a/b/c.txt'
	 * @return the constant pool index
	 */
	public int compressFilepath(String filepath) {
		if (constantPoolWriter == null) {
			throw new IllegalStateException();
		}
		return constantPoolWriter.writeUtf8(filepath);
	}

	/**
	 * @param name a simple name (for example a method or field name)
	 * @return the constant pool index
	 */
	public int compressName(String name) {
		if (constantPoolWriter == null) {
			throw new IllegalStateException();
		}
		return constantPoolWriter.writeUtf8(name);
	}

	/**
	 * @param name a simple name (for example a method or field name)
	 */
	public void writeCompressedName(String name) throws IOException {
		writeShort(compressName(name));
	}

	/**
	 * @param signature of the form 'La/b/c/d;'
	 */
	public void writeCompressedSignature(String signature) throws IOException {
		writeShort(compressSignature(signature));
	}

	/**
	 * @param path a file system path 'c:\a\b\c.txt' or '/a/b/c.txt'
	 */
	public void writeCompressedPath(String path) throws IOException {
		writeShort(compressFilepath(path));
	}

}