1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
177 * If the recycled instance implements {@link RecycleAware},
178 * {@link RecycleAware#onRecycled()} will be invoked to let the instance
179 * know that it's being recycled.
180 *
181 * <p>
182 * It is not a hard requirement to call this method on every {@link XMLStreamReader}
183 * instance. Not doing so just reduces the performance by throwing away
184 * possibly reusable instances. So the caller should always consider the effort
185 * it takes to recycle vs the possible performance gain by doing so.
186 *
187 * <p>
188 * This method may be invoked by multiple threads concurrently.
189 *
190 * @param r
191 * The {@link XMLStreamReader} instance that the caller finished using.
192 * This could be any {@link XMLStreamReader} implementation, not just
193 * the ones that were created from this factory. So the implementation
194 * of this class needs to be aware of that.
195 */
196 public static void recycle(XMLStreamReader r) {
197 get().doRecycle(r);
198 if (r instanceof RecycleAware) {
199 ((RecycleAware)r).onRecycled();
200 }
201 }
202
203 // implementations
204
205 public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs);
206
207 private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) {
208 Reader reader;
209 try {
210 reader = new InputStreamReader(in, encoding);
211 } catch(UnsupportedEncodingException ue) {
212 throw new XMLReaderException("stax.cantCreate", ue);
213 }
214 return doCreate(systemId, reader, rejectDTDs);
215 }
216
217 public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs);
218
219 public abstract void doRecycle(XMLStreamReader r);
220
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
177 * If the recycled instance implements {@link RecycleAware},
178 * {@link RecycleAware#onRecycled()} will be invoked to let the instance
179 * know that it's being recycled.
180 *
181 * <p>
182 * It is not a hard requirement to call this method on every {@link XMLStreamReader}
183 * instance. Not doing so just reduces the performance by throwing away
184 * possibly reusable instances. So the caller should always consider the effort
185 * it takes to recycle vs the possible performance gain by doing so.
186 *
187 * <p>
188 * This method may be invoked by multiple threads concurrently.
189 *
190 * @param r
191 * The {@link XMLStreamReader} instance that the caller finished using.
192 * This could be any {@link XMLStreamReader} implementation, not just
193 * the ones that were created from this factory. So the implementation
194 * of this class needs to be aware of that.
195 */
196 public static void recycle(XMLStreamReader r) {
197 /* the XMLStreamReaderFactory recycle becomes expenisve in the threadLocal get operation.
198 get().doRecycle(r);
199 if (r instanceof RecycleAware) {
200 ((RecycleAware)r).onRecycled();
201 }*/
202 }
203
204 // implementations
205
206 public abstract XMLStreamReader doCreate(String systemId, InputStream in, boolean rejectDTDs);
207
208 private XMLStreamReader doCreate(String systemId, InputStream in, @NotNull String encoding, boolean rejectDTDs) {
209 Reader reader;
210 try {
211 reader = new InputStreamReader(in, encoding);
212 } catch(UnsupportedEncodingException ue) {
213 throw new XMLReaderException("stax.cantCreate", ue);
214 }
215 return doCreate(systemId, reader, rejectDTDs);
216 }
217
218 public abstract XMLStreamReader doCreate(String systemId, Reader reader, boolean rejectDTDs);
219
220 public abstract void doRecycle(XMLStreamReader r);
221
|