001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: InternalString.java 3803 2007-06-22 22:15:25Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import com.uwyn.rife.tools.StringUtils;
011: import java.io.UnsupportedEncodingException;
012: import java.lang.ref.SoftReference;
013:
014: public class InternalString implements CharSequence {
015: private CharSequence mStringValue = null;
016:
017: private transient SoftReference<byte[]> mBytesValue_US_ASCII = null;
018: private transient SoftReference<byte[]> mBytesValue_ISO_8859_1 = null;
019: private transient SoftReference<byte[]> mBytesValue_UTF_8 = null;
020: private transient SoftReference<byte[]> mBytesValue_UTF_16 = null;
021: private transient SoftReference<byte[]> mBytesValue_UTF_16BE = null;
022: private transient SoftReference<byte[]> mBytesValue_UTF_16LE = null;
023:
024: public InternalString(String value) {
025: mStringValue = value;
026: }
027:
028: public InternalString(CharSequence value) {
029: mStringValue = value;
030: }
031:
032: public String toString() {
033: return mStringValue.toString();
034: }
035:
036: public byte[] getBytes(String charsetName)
037: throws UnsupportedEncodingException {
038: byte[] bytes = null;
039:
040: if (StringUtils.ENCODING_ISO_8859_1.equals(charsetName)) {
041: if (mBytesValue_ISO_8859_1 != null) {
042: bytes = mBytesValue_ISO_8859_1.get();
043: }
044: if (null == bytes) {
045: bytes = toString().getBytes(charsetName);
046: if (null == mBytesValue_ISO_8859_1) {
047: mBytesValue_ISO_8859_1 = new SoftReference<byte[]>(
048: bytes);
049: }
050: }
051: } else if (StringUtils.ENCODING_UTF_8.equals(charsetName)) {
052: if (mBytesValue_UTF_8 != null) {
053: bytes = mBytesValue_UTF_8.get();
054: }
055: if (null == bytes) {
056: bytes = toString().getBytes(charsetName);
057: if (null == mBytesValue_UTF_8) {
058: mBytesValue_UTF_8 = new SoftReference<byte[]>(bytes);
059: }
060: }
061: } else if (StringUtils.ENCODING_US_ASCII.equals(charsetName)) {
062: if (mBytesValue_US_ASCII != null) {
063: bytes = mBytesValue_US_ASCII.get();
064: }
065: if (null == bytes) {
066: bytes = toString().getBytes(charsetName);
067: if (null == mBytesValue_US_ASCII) {
068: mBytesValue_US_ASCII = new SoftReference<byte[]>(
069: bytes);
070: }
071: }
072: } else if (StringUtils.ENCODING_UTF_16.equals(charsetName)) {
073: if (mBytesValue_UTF_16 != null) {
074: bytes = mBytesValue_UTF_16.get();
075: }
076: if (null == bytes) {
077: bytes = toString().getBytes(charsetName);
078: if (null == mBytesValue_UTF_16) {
079: mBytesValue_UTF_16 = new SoftReference<byte[]>(
080: bytes);
081: }
082: }
083: } else if (StringUtils.ENCODING_UTF_16BE.equals(charsetName)) {
084: if (mBytesValue_UTF_16BE != null) {
085: bytes = mBytesValue_UTF_16BE.get();
086: }
087: if (null == bytes) {
088: bytes = toString().getBytes(charsetName);
089: if (null == mBytesValue_UTF_16BE) {
090: mBytesValue_UTF_16BE = new SoftReference<byte[]>(
091: bytes);
092: }
093: }
094: } else if (StringUtils.ENCODING_UTF_16LE.equals(charsetName)) {
095: if (mBytesValue_UTF_16LE != null) {
096: bytes = mBytesValue_UTF_16LE.get();
097: }
098: if (null == bytes) {
099: bytes = toString().getBytes(charsetName);
100: if (null == mBytesValue_UTF_16LE) {
101: mBytesValue_UTF_16LE = new SoftReference<byte[]>(
102: bytes);
103: }
104: }
105: } else {
106: bytes = toString().getBytes(charsetName);
107: }
108:
109: return bytes;
110: }
111:
112: public int length() {
113: return mStringValue.length();
114: }
115:
116: public void append(String value) {
117: mStringValue = mStringValue + value;
118: if (mBytesValue_ISO_8859_1 != null) {
119: SoftReference<byte[]> reference = mBytesValue_ISO_8859_1;
120: mBytesValue_ISO_8859_1 = null;
121: reference.clear();
122: }
123: if (mBytesValue_UTF_8 != null) {
124: SoftReference<byte[]> reference = mBytesValue_UTF_8;
125: mBytesValue_UTF_8 = null;
126: reference.clear();
127: }
128: if (mBytesValue_UTF_16 != null) {
129: SoftReference<byte[]> reference = mBytesValue_UTF_16;
130: mBytesValue_UTF_16 = null;
131: reference.clear();
132: }
133: if (mBytesValue_UTF_16BE != null) {
134: SoftReference<byte[]> reference = mBytesValue_UTF_16BE;
135: mBytesValue_UTF_16BE = null;
136: reference.clear();
137: }
138: if (mBytesValue_UTF_16LE != null) {
139: SoftReference<byte[]> reference = mBytesValue_UTF_16LE;
140: mBytesValue_UTF_16LE = null;
141: reference.clear();
142: }
143: }
144:
145: public CharSequence subSequence(int beginIndex, int endIndex) {
146: return mStringValue.subSequence(beginIndex, endIndex);
147: }
148:
149: public char charAt(int index) {
150: return mStringValue.charAt(index);
151: }
152: }
|