jdk/src/share/classes/sun/nio/cs/ThreadLocalCoders.java
author martin
Mon, 31 Aug 2009 15:00:04 -0700
changeset 3714 6a4eb8f53f91
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6860431: Character.isSurrogate(char ch) Summary: Add new method Character.isSurrogate(char ch) Reviewed-by: sherman, darcy, okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Utility class for caching per-thread decoders and encoders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ThreadLocalCoders {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final int CACHE_SIZE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static abstract class Cache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        // Thread-local reference to array of cached objects, in LRU order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private ThreadLocal cache = new ThreadLocal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        private final int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Cache(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        abstract Object create(Object name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        private void moveToFront(Object[] oa, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            Object ob = oa[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            for (int j = i; j > 0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                oa[j] = oa[j - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            oa[0] = ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        abstract boolean hasName(Object ob, Object name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Object forName(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            Object[] oa = (Object[])cache.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (oa == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                oa = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                cache.set(oa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                for (int i = 0; i < oa.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    Object ob = oa[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    if (ob == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    if (hasName(ob, name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            moveToFront(oa, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        return ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // Create a new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            Object ob = create(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            oa[oa.length - 1] = ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            moveToFront(oa, oa.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static Cache decoderCache = new Cache(CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            boolean hasName(Object ob, Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (name instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return (((CharsetDecoder)ob).charset().name().equals(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (name instanceof Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return ((CharsetDecoder)ob).charset().equals(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            Object create(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (name instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    return Charset.forName((String)name).newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (name instanceof Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    return ((Charset)name).newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static CharsetDecoder decoderFor(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        CharsetDecoder cd = (CharsetDecoder)decoderCache.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        cd.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return cd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static Cache encoderCache = new Cache(CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            boolean hasName(Object ob, Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (name instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return (((CharsetEncoder)ob).charset().name().equals(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (name instanceof Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    return ((CharsetEncoder)ob).charset().equals(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Object create(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (name instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    return Charset.forName((String)name).newEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (name instanceof Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    return ((Charset)name).newEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static CharsetEncoder encoderFor(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        CharsetEncoder ce = (CharsetEncoder)encoderCache.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ce.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}