| ByteArrayMaker.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.kernel.util;
24
25 import java.io.ByteArrayOutputStream;
26
27 /**
28 * <a href="ByteArrayMaker.java.html"><b><i>View Source</i></b></a>
29 *
30 * @author Harry Mark
31 *
32 */
33 public class ByteArrayMaker extends ByteArrayOutputStream {
34
35 static boolean collect = false;
36
37 static {
38 String collectString = System.getProperty(MakerStats.class.getName());
39
40 if (collectString != null) {
41 if (collectString.equals("true")) {
42 collect = true;
43 }
44 }
45 }
46
47 static MakerStats stats = null;
48
49 static {
50 if (collect) {
51 stats = new MakerStats(ByteArrayMaker.class.toString());
52 }
53 }
54
55 static int defaultInitSize = 8000;
56
57 static {
58 String defaultInitSizeString = System.getProperty(
59 ByteArrayMaker.class.getName() + ".initial.size");
60
61 if (defaultInitSizeString != null) {
62 try {
63 defaultInitSize = Integer.parseInt(defaultInitSizeString);
64 }
65 catch (Exception e) {
66 e.printStackTrace();
67 }
68 }
69 }
70
71 public static MakerStats getStatistics() {
72 return stats;
73 }
74
75 public ByteArrayMaker() {
76 super(defaultInitSize);
77
78 if (collect) {
79 _getInfo(new Throwable());
80 }
81 }
82
83 public ByteArrayMaker(int size) {
84 super(size);
85
86 if (collect) {
87 _getInfo(new Throwable());
88 }
89 }
90
91 public byte[] toByteArray() {
92 if (collect) {
93 stats.add(_caller, _initSize, count);
94 }
95
96 return super.toByteArray();
97 }
98
99 public String toString() {
100 return super.toString();
101 }
102
103 private void _getInfo(Throwable t) {
104 _initSize = buf.length;
105
106 StackTraceElement[] elements = t.getStackTrace();
107
108 if (elements.length > 1) {
109 StackTraceElement el = elements[1];
110
111 _caller =
112 el.getClassName() + StringPool.PERIOD + el.getMethodName() +
113 StringPool.COLON + el.getLineNumber();
114 }
115 }
116
117 private int _initSize;
118 private String _caller;
119
120 }