1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.base;
33
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35 import java.lang.ref.Reference;
36 import java.lang.ref.SoftReference;
37
38 /**
39 *
40 * @author zoly
41 */
42 public final class TLScratch {
43
44 private static final int MAX_LOCAL_ARRAY_SIZE = Integer.getInteger("spf4j.TLScratch.maxLocalArraySize", 256 * 1024);
45
46 private static final ThreadLocal<Reference<byte[]>> BYTES_TMP = new ThreadLocal<>();
47
48 private static final ThreadLocal<Reference<char[]>> CHARS_TMP = new ThreadLocal<>();
49
50 private TLScratch() { }
51
52 /**
53 * returns a thread local byte array of at least the size requested. use only for temporary purpose. This method needs
54 * to be carefully used!
55 *
56 * @param size - the minimum size of the temporary buffer requested.
57 * @return - the temporary buffer.
58 */
59 public static byte[] getBytesTmp(final int size) {
60 if (size > MAX_LOCAL_ARRAY_SIZE) {
61 return new byte[size];
62 }
63 Reference<byte[]> sr = BYTES_TMP.get();
64 byte[] result;
65 if (sr == null) {
66 result = new byte[size];
67 BYTES_TMP.set(new SoftReference<>(result));
68 } else {
69 result = sr.get();
70 if (result == null || result.length < size) {
71 result = new byte[size];
72 BYTES_TMP.set(new SoftReference<>(result));
73 }
74 }
75 return result;
76 }
77
78 /**
79 * returns a thread local char array of at least the requested size. Use only for temporary purpose.
80 *
81 * @param size - the minimum size of the temporary buffer requested.
82 * @return - the temporary buffer.
83 */
84 @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
85 public static char[] getCharsTmp(final int size) {
86 if (size > MAX_LOCAL_ARRAY_SIZE) {
87 return new char[size];
88 }
89 Reference<char[]> sr = CHARS_TMP.get();
90 char[] result;
91 if (sr == null) {
92 result = new char[size];
93 CHARS_TMP.set(new SoftReference<>(result));
94 } else {
95 result = sr.get();
96 if (result == null || result.length < size) {
97 result = new char[size];
98 CHARS_TMP.set(new SoftReference<>(result));
99 }
100 }
101 return result;
102 }
103
104 }