Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 413
نمايش فراداده

CharBufferjava.nio

Java 1.4comparable appendable readable

CharBuffer holds a sequence of Unicode character values for use in an I/O operation. Most of the methods of this class are directly analogous to methods defined by ByteBuffer except that they use char and char[ ] argument and return values instead of byte and byte[ ] values. See ByteBuffer for details.

In addition to the ByteBuffer analogs, this class also implements the java.lang.CharSequence interface so that it can be used with java.util.regex regular expression operations or anywhere else a CharSequence is expected. In Java 5.0, CharBuffer adds the append( ) and read( ) methods of the java.lang.Appendable and java.lang.Readable interfaces, making CharBuffer objects suitable for use with the Formatter and Scanner classes of java.util.

Note that CharBuffer is an abstract class and does not defined a constructor. There are three ways to obtain a CharBuffer:

  • By calling the static allocate( ) method. Note that there is no allocateDirect( ) method as there is for ByteBuffer.

  • By calling one of the static wrap( ) methods to create a CharBuffer that uses the specified char array or CharSequence for its content. Note that wrapping a CharSequence results in a read-only CharBuffer.

  • By calling the asCharBuffer( ) method of a ByteBuffer to obtain a CharBuffer "view" of the underlying bytes. If the underlying ByteBuffer is direct, then the CharBuffer view will also be direct.

Note that this class holds a sequence of 16-bit Unicode characters, and does not represent text in any other encoding. Classes in the java.nio.charset package can be used to encode a CharBuffer of Unicode characters into a ByteBuffer, or decode the bytes in a ByteBuffer into a CharBuffer of Unicode text. Java 5.0 supports Unicode supplementary characters that do not fit in 16 bits. See java.lang.Character for details. Note that CharBuffer does not include any utility methods for working with int codepoints or surrogate pairs.

Figure 13-4. java.nio.CharBuffer

public abstract class

CharBuffer extends Buffer implements Comparable<CharBuffer>, Appendable, CharSequence, Readable { // No Constructor // Public Class Methods public static CharBuffer

allocate (int

capacity ); public static CharBuffer

wrap (char[ ]

array ); public static CharBuffer

wrap (CharSequence

csq ); public static CharBuffer

wrap (char[ ]

array , int

offset , int

length ); public static CharBuffer

wrap (CharSequence

csq , int

start , int

end ); // Public Instance Methods

5.0 public CharBuffer

append (char

c );

5.0 public CharBuffer

append (CharSequence

csq );

5.0 public CharBuffer

append (CharSequence

csq , int

start , int

end ); public final char[ ]

array ( ); public final int

arrayOffset ( ); public abstract CharBuffer

asReadOnlyBuffer ( ); public abstract CharBuffer

compact ( ); public abstract CharBuffer

duplicate ( ); public abstract char

get ( ); public abstract char

get (int

index ); public CharBuffer

get (char[ ]

dst ); public CharBuffer

get (char[ ]

dst , int

offset , int

length ); public final boolean

hasArray ( ); public abstract boolean

isDirect ( ); public abstract ByteOrder

order ( ); public final CharBuffer

put (char[ ]

src ); public CharBuffer

put (CharBuffer

src ); public final CharBuffer

put (String

src ); public abstract CharBuffer

put (char

c ); public abstract CharBuffer

put (int

index , char

c ); public CharBuffer

put (String

src , int

start , int

end ); public CharBuffer

put (char[ ]

src , int

offset , int

length ); public abstract CharBuffer

slice ( ); // Methods Implementing CharSequence public final char

charAt (int

index ); public final int

length ( ); public abstract CharSequence

subSequence (int

start , int

end ); public String

toString ( ); // Methods Implementing Comparable

5.0 public int

compareTo (CharBuffer

that ); // Methods Implementing Readable

5.0 public int

read (CharBuffer

target ) throws java.io.IOException; // Public Methods Overriding Object public boolean

equals (Object

ob ); public int

hashCode ( ); }

Passed To

java.io.Reader.read( ), Readable.read( ), java.nio.charset.Charset.encode( ), java.nio.charset.CharsetDecoder.{decode( ), decodeLoop( ), flush( ), implFlush( )}, java.nio.charset.CharsetEncoder.{encode( ), encodeLoop( )}

Returned By

ByteBuffer.asCharBuffer( ), java.nio.charset.Charset.decode( ), java.nio.charset.CharsetDecoder.decode( )