/
BufferRecyclers.java
165 lines (148 loc) · 5.81 KB
/
BufferRecyclers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.fasterxml.jackson.core.util;
import java.lang.ref.SoftReference;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
/**
* Helper entity used to control access to simple buffer recyling scheme used for
* some encoding, decoding tasks.
*
* @see BufferRecycler
*
* @since 2.9.2
*/
public class BufferRecyclers
{
/**
* System property that is checked to see if recycled buffers (see {@link BufferRecycler})
* should be tracked, for purpose of forcing release of all such buffers, typically
* during major classloading.
*
* @since 2.9.6
*/
public final static String SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS
= "com.fasterxml.jackson.core.util.BufferRecyclers.trackReusableBuffers";
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
/**
* Flag that indicates whether {@link BufferRecycler} instances should be tracked.
*/
private final static ThreadLocalBufferManager _bufferRecyclerTracker;
static {
boolean trackReusableBuffers = false;
try {
trackReusableBuffers = "true".equals(System.getProperty(SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS));
} catch (SecurityException e) { }
_bufferRecyclerTracker = trackReusableBuffers ? ThreadLocalBufferManager.instance() : null;
}
/*
/**********************************************************
/* BufferRecyclers for parsers, generators
/**********************************************************
*/
/**
* This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
* to a {@link BufferRecycler} used to provide a low-cost
* buffer recycling between reader and writer instances.
*/
final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
= new ThreadLocal<SoftReference<BufferRecycler>>();
/**
* Main accessor to call for accessing possibly recycled {@link BufferRecycler} instance.
*/
public static BufferRecycler getBufferRecycler()
{
SoftReference<BufferRecycler> ref = _recyclerRef.get();
BufferRecycler br = (ref == null) ? null : ref.get();
if (br == null) {
br = new BufferRecycler();
if (_bufferRecyclerTracker != null) {
ref = _bufferRecyclerTracker.wrapAndTrack(br);
} else {
ref = new SoftReference<BufferRecycler>(br);
}
_recyclerRef.set(ref);
}
return br;
}
/**
* Specialized method that will release all recycled {@link BufferRecycler} if
* (and only if) recycler tracking has been enabled
* (see {@link #SYSTEM_PROPERTY_TRACK_REUSABLE_BUFFERS}).
* This method is usually called on shutdown of the container like Application Server
* to ensure that no references are reachable via {@link ThreadLocal}s as this may cause
* unintentional retention of sizable amounts of memory. It may also be called regularly
* if GC for some reason does not clear up {@link SoftReference}s aggressively enough.
*
* @return Number of buffers released, if tracking enabled (zero or more); -1 if tracking not enabled.
*
* @since 2.9.6
*/
public static int releaseBuffers() {
if (_bufferRecyclerTracker != null) {
return _bufferRecyclerTracker.releaseBuffers();
}
return -1;
}
/*
/**********************************************************************
/* Obsolete things re-introduced in 2.12.5 after accidental direct
/* removal from 2.10.0
/**********************************************************************
*/
/**
* Not to be used any more: call {@link JsonStringEncoder#getInstance()} instead.
*
* @deprecated Since 2.10 (note: was accidentally removed but reintroduced as deprecated
* in 2.12.5, to be removed from 3.0)
*/
@Deprecated
public static JsonStringEncoder getJsonStringEncoder() {
return JsonStringEncoder.getInstance();
}
/**
* Not to be used any more: call {@link JsonStringEncoder#getInstance()} (and then
* {@code encodeAsUTF8()}) instead.
*
* @deprecated Since 2.10 (note: was accidentally removed but reintroduced as deprecated
* in 2.12.5, to be removed from 3.0)
*/
@Deprecated
public static byte[] encodeAsUTF8(String text) {
return JsonStringEncoder.getInstance().encodeAsUTF8(text);
}
/**
* Not to be used any more: call {@link JsonStringEncoder#getInstance()} (and then
* {@code quoteAsString()}) instead.
*
* @deprecated Since 2.10 (note: was accidentally removed but reintroduced as deprecated
* in 2.12.5, to be removed from 3.0)
*/
@Deprecated
public static char[] quoteAsJsonText(String rawText) {
return JsonStringEncoder.getInstance().quoteAsString(rawText);
}
/**
* Not to be used any more: call {@link JsonStringEncoder#getInstance()} (and then
* {@code quoteAsString()}) instead.
*
* @deprecated Since 2.10 (note: was accidentally removed but reintroduced as deprecated
* in 2.12.5, to be removed from 3.0)
*/
@Deprecated
public static void quoteAsJsonText(CharSequence input, StringBuilder output) {
JsonStringEncoder.getInstance().quoteAsString(input, output);
}
/**
* Not to be used any more: call {@link JsonStringEncoder#getInstance()} (and then
* {@code quoteAsUTF8()}) instead.
*
* @deprecated Since 2.10 (note: was accidentally removed but reintroduced as deprecated
* in 2.12.5, to be removed from 3.0)
*/
@Deprecated
public static byte[] quoteAsJsonUTF8(String rawText) {
return JsonStringEncoder.getInstance().quoteAsUTF8(rawText);
}
}