1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.ByteArrayOutputStream;
23  
24  /**
25   * <a href="ByteArrayMaker.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Harry Mark
28   *
29   */
30  public class ByteArrayMaker extends ByteArrayOutputStream {
31  
32      static boolean collect = false;
33  
34      static {
35          String collectString = System.getProperty(MakerStats.class.getName());
36  
37          if (collectString != null) {
38              if (collectString.equals("true")) {
39                  collect = true;
40              }
41          }
42      }
43  
44      static MakerStats stats = null;
45  
46      static {
47          if (collect) {
48              stats = new MakerStats(ByteArrayMaker.class.toString());
49          }
50      }
51  
52      static int defaultInitSize = 8000;
53  
54      static {
55          String defaultInitSizeString = System.getProperty(
56              ByteArrayMaker.class.getName() + ".initial.size");
57  
58          if (defaultInitSizeString != null) {
59              try {
60                  defaultInitSize = Integer.parseInt(defaultInitSizeString);
61              }
62              catch (Exception e) {
63                  e.printStackTrace();
64              }
65          }
66      }
67  
68      public static MakerStats getStatistics() {
69          return stats;
70      }
71  
72      public ByteArrayMaker() {
73          super(defaultInitSize);
74  
75          if (collect) {
76              _getInfo(new Throwable());
77          }
78      }
79  
80      public ByteArrayMaker(int size) {
81          super(size);
82  
83          if (collect) {
84              _getInfo(new Throwable());
85          }
86      }
87  
88      public byte[] toByteArray() {
89          if (collect) {
90              stats.add(_caller, _initSize, count);
91          }
92  
93          return super.toByteArray();
94      }
95  
96      public String toString() {
97          return super.toString();
98      }
99  
100     private void _getInfo(Throwable t) {
101         _initSize = buf.length;
102 
103         StackTraceElement[] elements = t.getStackTrace();
104 
105         if (elements.length > 1) {
106             StackTraceElement el = elements[1];
107 
108             _caller =
109                 el.getClassName() + StringPool.PERIOD + el.getMethodName() +
110                     StringPool.COLON + el.getLineNumber();
111         }
112     }
113 
114     private int _initSize;
115     private String _caller;
116 
117 }