001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.io.ByteArrayOutputStream;
024:
025: /**
026: * <a href="ByteArrayMaker.java.html"><b><i>View Source</i></b></a>
027: *
028: * @author Harry Mark
029: *
030: */
031: public class ByteArrayMaker extends ByteArrayOutputStream {
032:
033: static boolean collect = false;
034:
035: static {
036: String collectString = System.getProperty(MakerStats.class
037: .getName());
038:
039: if (collectString != null) {
040: if (collectString.equals("true")) {
041: collect = true;
042: }
043: }
044: }
045:
046: static MakerStats stats = null;
047:
048: static {
049: if (collect) {
050: stats = new MakerStats(ByteArrayMaker.class.toString());
051: }
052: }
053:
054: static int defaultInitSize = 8000;
055:
056: static {
057: String defaultInitSizeString = System
058: .getProperty(ByteArrayMaker.class.getName()
059: + ".initial.size");
060:
061: if (defaultInitSizeString != null) {
062: try {
063: defaultInitSize = Integer
064: .parseInt(defaultInitSizeString);
065: } catch (Exception e) {
066: e.printStackTrace();
067: }
068: }
069: }
070:
071: public static MakerStats getStatistics() {
072: return stats;
073: }
074:
075: public ByteArrayMaker() {
076: super (defaultInitSize);
077:
078: if (collect) {
079: _getInfo(new Throwable());
080: }
081: }
082:
083: public ByteArrayMaker(int size) {
084: super (size);
085:
086: if (collect) {
087: _getInfo(new Throwable());
088: }
089: }
090:
091: public byte[] toByteArray() {
092: if (collect) {
093: stats.add(_caller, _initSize, count);
094: }
095:
096: return super .toByteArray();
097: }
098:
099: 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 = el.getClassName() + StringPool.PERIOD
112: + el.getMethodName() + StringPool.COLON
113: + el.getLineNumber();
114: }
115: }
116:
117: private int _initSize;
118: private String _caller;
119:
120: }
|