Commit 3386451d by Jesse Rosenstock Committed by Michael Koch

2002-11-17 Jesse Rosenstock <jmr@ugcs.caltech.edu>

	* java/nio/charset/Charset.java
	(<clinit>): New method.
	(encode): Synchronize use of cached encoder object.
	(decode): Synchronize use of cached encoder object.

From-SVN: r59218
parent ea4210ef
2002-11-17 Jesse Rosenstock <jmr@ugcs.caltech.edu>
* java/nio/charset/Charset.java
(<clinit>): New method.
(encode): Synchronize use of cached encoder object.
(decode): Synchronize use of cached encoder object.
2002-11-18 Michael Koch <konqueror@gmx.de> 2002-11-18 Michael Koch <konqueror@gmx.de>
* gnu/java/nio/ByteBufferImpl.java, * gnu/java/nio/ByteBufferImpl.java,
......
...@@ -55,6 +55,18 @@ import gnu.java.nio.charset.Provider; ...@@ -55,6 +55,18 @@ import gnu.java.nio.charset.Provider;
*/ */
public abstract class Charset implements Comparable public abstract class Charset implements Comparable
{ {
private static CharsetEncoder cachedEncoder;
private static CharsetDecoder cachedDecoder;
static
{
synchronized (Charset.class)
{
cachedEncoder = null;
cachedDecoder = null;
}
}
private final String canonicalName; private final String canonicalName;
private final String[] aliases; private final String[] aliases;
...@@ -195,10 +207,21 @@ public abstract class Charset implements Comparable ...@@ -195,10 +207,21 @@ public abstract class Charset implements Comparable
{ {
try try
{ {
// TODO: cache encoders between sucessive invocations // NB: This implementation serializes different threads calling
return newEncoder ().onMalformedInput (CodingErrorAction.REPLACE) // Charset.encode(), a potential performance problem. It might
.onUnmappableCharacter (CodingErrorAction.REPLACE) // be better to remove the cache, or use ThreadLocal to cache on
.encode (cb); // a per-thread basis.
synchronized (Charset.class)
{
if (cachedEncoder == null)
{
cachedEncoder = newEncoder ()
.onMalformedInput (CodingErrorAction.REPLACE)
.onUnmappableCharacter (CodingErrorAction.REPLACE);
}
return cachedEncoder.encode (cb);
}
} }
catch (CharacterCodingException e) catch (CharacterCodingException e)
{ {
...@@ -215,10 +238,21 @@ public abstract class Charset implements Comparable ...@@ -215,10 +238,21 @@ public abstract class Charset implements Comparable
{ {
try try
{ {
// TODO: cache encoders between sucessive invocations // NB: This implementation serializes different threads calling
return newDecoder ().onMalformedInput (CodingErrorAction.REPLACE) // Charset.decode(), a potential performance problem. It might
.onUnmappableCharacter (CodingErrorAction.REPLACE) // be better to remove the cache, or use ThreadLocal to cache on
.decode (bb); // a per-thread basis.
synchronized (Charset.class)
{
if (cachedDecoder == null)
{
cachedDecoder = newDecoder ()
.onMalformedInput (CodingErrorAction.REPLACE)
.onUnmappableCharacter (CodingErrorAction.REPLACE);
}
return cachedDecoder.decode (bb);
}
} }
catch (CharacterCodingException e) catch (CharacterCodingException e)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment