You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/07/31 10:04:44 UTC
svn commit: r226615 -
/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java
Author: elecharny
Date: Sun Jul 31 01:04:41 2005
New Revision: 226615
URL: http://svn.apache.org/viewcvs?rev=226615&view=rev
Log:
The length now contains the number of bytes of the string, not the number of chars.
Added the method to get this length
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java
Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java?rev=226615&r1=226614&r2=226615&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java Sun Jul 31 01:04:41 2005
@@ -17,7 +17,7 @@
package org.apache.asn1.util;
/**
- * A Mutable version of the String class. This mutable Strins can be
+ * A Mutable version of the String class. This mutable String can be
* stored in a pool.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
@@ -26,7 +26,11 @@
/** The string is stored in a char array */
private char[] string;
- /** Actual length of the string */
+ /** We also store the byte array for performance sake */
+ private byte[] data;
+
+ /** Actual length of the string. Be aware that it's a number of bytes,
+ * not a number of chars ! */
private int length;
/** A null MutableString */
@@ -50,7 +54,7 @@
*/
public MutableString(String string)
{
- this(string.getBytes());
+ this( string.getBytes() );
}
/**
@@ -80,15 +84,7 @@
*/
public MutableString(byte[] bytes)
{
- length = StringUtils.countChars(bytes);
- string = new char[length];
- int pos = 0;
-
- for ( int i = 0; i < length; i++ )
- {
- string[i] = StringUtils.bytesToChar(bytes, pos);
- pos += StringUtils.countBytesPerChar(bytes, pos);
- }
+ setData( bytes );
}
/**
@@ -99,13 +95,15 @@
*/
public void setData(byte[] bytes)
{
- length = StringUtils.countChars(bytes);
+ data = bytes;
+ length = bytes.length;
+ int charLength = StringUtils.countChars(bytes);
- string = new char[length];
+ string = new char[charLength];
int pos = 0;
- for ( int i = 0; i < length; i++ )
+ for ( int i = 0; i < charLength; i++ )
{
string[i] = StringUtils.bytesToChar(bytes, pos);
pos += StringUtils.countBytesPerChar(bytes, pos);
@@ -113,7 +111,8 @@
}
/**
- * @return Returns the length.
+ * @return Returns the length, as a number of bytes, not a number
+ * of chars.
*/
public int getLength()
{
@@ -121,10 +120,18 @@
}
/**
+ * @return Returns the data.
+ */
+ public byte[] getData()
+ {
+ return data;
+ }
+
+ /**
* Return a native String representation of the MutableString.
*/
public String toString()
{
- return length == 0 ? "" : new String(string, 0, length);
+ return length == 0 ? "" : new String(data, 0, length);
}
}