1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.ByteArrayOutputStream;
18  
19  /**
20   * <a href="ByteArrayMaker.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author     Harry Mark
23   * @deprecated
24   */
25  public class ByteArrayMaker extends ByteArrayOutputStream {
26  
27      static boolean collect = false;
28  
29      static {
30          String collectString = System.getProperty(MakerStats.class.getName());
31  
32          if (collectString != null) {
33              if (collectString.equals("true")) {
34                  collect = true;
35              }
36          }
37      }
38  
39      static MakerStats stats = null;
40  
41      static {
42          if (collect) {
43              stats = new MakerStats(ByteArrayMaker.class.toString());
44          }
45      }
46  
47      static int defaultInitSize = 8000;
48  
49      static {
50          String defaultInitSizeString = System.getProperty(
51              ByteArrayMaker.class.getName() + ".initial.size");
52  
53          if (defaultInitSizeString != null) {
54              try {
55                  defaultInitSize = Integer.parseInt(defaultInitSizeString);
56              }
57              catch (Exception e) {
58                  e.printStackTrace();
59              }
60          }
61      }
62  
63      public static MakerStats getStatistics() {
64          return stats;
65      }
66  
67      public ByteArrayMaker() {
68          super(defaultInitSize);
69  
70          if (collect) {
71              _getInfo(new Throwable());
72          }
73      }
74  
75      public ByteArrayMaker(int size) {
76          super(size);
77  
78          if (collect) {
79              _getInfo(new Throwable());
80          }
81      }
82  
83      public byte[] toByteArray() {
84          if (collect) {
85              stats.add(_caller, _initSize, count);
86          }
87  
88          return super.toByteArray();
89      }
90  
91      public String toString() {
92          return super.toString();
93      }
94  
95      private void _getInfo(Throwable t) {
96          _initSize = buf.length;
97  
98          StackTraceElement[] elements = t.getStackTrace();
99  
100         if (elements.length > 1) {
101             StackTraceElement el = elements[1];
102 
103             _caller =
104                 el.getClassName() + StringPool.PERIOD + el.getMethodName() +
105                     StringPool.COLON + el.getLineNumber();
106         }
107     }
108 
109     private int _initSize;
110     private String _caller;
111 
112 }