classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

java.nio.CharBuffer should implement java.lang.CharSequence


From: Jesse Rosenstock
Subject: java.nio.CharBuffer should implement java.lang.CharSequence
Date: Fri, 22 Nov 2002 15:51:00 -0800

According to Sun's javadoc, the CharSequence methods in CharBuffer
should all deal with the remaining portion of the buffer, ie the
positions in [position(), limit()[.  

Index: gnu/java/nio/CharBufferImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/CharBufferImpl.java,v
retrieving revision 1.10
diff -c -u -r1.10 CharBufferImpl.java
--- gnu/java/nio/CharBufferImpl.java    22 Nov 2002 13:06:50 -0000      1.10
+++ gnu/java/nio/CharBufferImpl.java    22 Nov 2002 22:20:47 -0000
@@ -132,8 +132,14 @@
 
   final public CharSequence subSequence (int start, int end)
   {
-    // FIXME
-    return null;
+    if (start < 0 || end > length () || start > end)
+      throw new IndexOutOfBoudsException ();
+
+    // No support for direct buffers yet.
+    // assert array () != null;
+    return new CharBufferImpl (array (),
+                               position () + start,
+                               position () + end);
   }
   
   final public char get()
@@ -167,14 +173,4 @@
   final public long getLong() { long a = nio_get_Long(this, position(), 
limit()); inc_pos(8); return a; } final public CharBuffer putLong(long value) { 
nio_put_Long(this, position(), limit(), value); inc_pos(8); return this; } 
final public long getLong(int index) { long a = nio_get_Long(this, index, 
limit()); return a; } final public CharBuffer putLong(int index, long value) { 
nio_put_Long(this, index, limit(), value); return this; };
   final public float getFloat() { float a = nio_get_Float(this, position(), 
limit()); inc_pos(4); return a; } final public CharBuffer putFloat(float value) 
{ nio_put_Float(this, position(), limit(), value); inc_pos(4); return this; } 
final public float getFloat(int index) { float a = nio_get_Float(this, index, 
limit()); return a; } final public CharBuffer putFloat(int index, float value) 
{ nio_put_Float(this, index, limit(), value); return this; };
   final public double getDouble() { double a = nio_get_Double(this, 
position(), limit()); inc_pos(8); return a; } final public CharBuffer 
putDouble(double value) { nio_put_Double(this, position(), limit(), value); 
inc_pos(8); return this; } final public double getDouble(int index) { double a 
= nio_get_Double(this, index, limit()); return a; } final public CharBuffer 
putDouble(int index, double value) { nio_put_Double(this, index, limit(), 
value); return this; };
-
-  public String toString()
-  {
-    if (backing_buffer != null)
-      {
-        return new String(backing_buffer, position(), limit());
-      }
-      
-    return super.toString();
-  }
 }
Index: java/nio/CharBuffer.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/nio/CharBuffer.java,v
retrieving revision 1.6
diff -c -u -r1.6 CharBuffer.java
--- java/nio/CharBuffer.java    22 Nov 2002 13:06:50 -0000      1.6
+++ java/nio/CharBuffer.java    22 Nov 2002 22:20:47 -0000
@@ -160,29 +160,25 @@
 
   public final int length ()
   {
-    return limit ();
+    return remaining ();
   }
 
   public final char charAt (int i)
   {
-    if (hasArray ())
-      {
-       return backing_buffer[i];
-      }
-
-    // FIXME: there must be a more elegant way of doing this.
-    return toString ().charAt (i);
+    if (i < 0 || i >= length ())
+      throw new IndexOutOfBoundsException ();
+    return get (position () + i);
   }
 
   public String toString()
   {
     if (hasArray ())
-      {
-       return new String (backing_buffer);
-      }
+      return new String (array (), position (), length ());
 
-    // FIXME: Implement this.
-    return "";
+    StringBuffer sb = new StringBuffer (length ());
+    for (int i = position (); i < limit (); ++i)
+      sb.append (get (i));
+    return sb.toString ();
   }
  
   public int compareTo(Object obj)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]