< prev index next >

src/java.base/share/classes/sun/nio/cs/Surrogate.java

Print this page




 158             return error;
 159         }
 160 
 161         /**
 162          * Returns an unmappable-input result object, with the appropriate
 163          * input length, for the previously-parsed character.
 164          */
 165         public CoderResult unmappableResult() {
 166             assert (error == null);
 167             return CoderResult.unmappableForLength(isPair ? 2 : 1);
 168         }
 169 
 170         /**
 171          * Parses a UCS-4 character from the given source buffer, handling
 172          * surrogates.
 173          *
 174          * @param  c    The first character
 175          * @param  in   The source buffer, from which one more character
 176          *              will be consumed if c is a high surrogate
 177          *
 178          * @returns  Either a parsed UCS-4 character, in which case the isPair()
 179          *           and increment() methods will return meaningful values, or
 180          *           -1, in which case error() will return a descriptive result
 181          *           object
 182          */
 183         public int parse(char c, CharBuffer in) {
 184             if (Character.isHighSurrogate(c)) {
 185                 if (!in.hasRemaining()) {
 186                     error = CoderResult.UNDERFLOW;
 187                     return -1;
 188                 }
 189                 char d = in.get();
 190                 if (Character.isLowSurrogate(d)) {
 191                     character = Character.toCodePoint(c, d);
 192                     isPair = true;
 193                     error = null;
 194                     return character;
 195                 }
 196                 error = CoderResult.malformedForLength(1);
 197                 return -1;
 198             }
 199             if (Character.isLowSurrogate(c)) {
 200                 error = CoderResult.malformedForLength(1);
 201                 return -1;
 202             }
 203             character = c;
 204             isPair = false;
 205             error = null;
 206             return character;
 207         }
 208 
 209         /**
 210          * Parses a UCS-4 character from the given source buffer, handling
 211          * surrogates.
 212          *
 213          * @param  c    The first character
 214          * @param  ia   The input array, from which one more character
 215          *              will be consumed if c is a high surrogate
 216          * @param  ip   The input index
 217          * @param  il   The input limit
 218          *
 219          * @returns  Either a parsed UCS-4 character, in which case the isPair()
 220          *           and increment() methods will return meaningful values, or
 221          *           -1, in which case error() will return a descriptive result
 222          *           object
 223          */
 224         public int parse(char c, char[] ia, int ip, int il) {
 225             assert (ia[ip] == c);
 226             if (Character.isHighSurrogate(c)) {
 227                 if (il - ip < 2) {
 228                     error = CoderResult.UNDERFLOW;
 229                     return -1;
 230                 }
 231                 char d = ia[ip + 1];
 232                 if (Character.isLowSurrogate(d)) {
 233                     character = Character.toCodePoint(c, d);
 234                     isPair = true;
 235                     error = null;
 236                     return character;
 237                 }
 238                 error = CoderResult.malformedForLength(1);
 239                 return -1;


 263 
 264         /**
 265          * If the previous generation operation detected an error, return the
 266          * object describing that error.
 267          */
 268         public CoderResult error() {
 269             assert error != null;
 270             return error;
 271         }
 272 
 273         /**
 274          * Generates one or two UTF-16 characters to represent the given UCS-4
 275          * character.
 276          *
 277          * @param  uc   The UCS-4 character
 278          * @param  len  The number of input bytes from which the UCS-4 value
 279          *              was constructed (used when creating result objects)
 280          * @param  dst  The destination buffer, to which one or two UTF-16
 281          *              characters will be written
 282          *
 283          * @returns  Either a positive count of the number of UTF-16 characters
 284          *           written to the destination buffer, or -1, in which case
 285          *           error() will return a descriptive result object
 286          */
 287         public int generate(int uc, int len, CharBuffer dst) {
 288             if (Character.isBmpCodePoint(uc)) {
 289                 char c = (char) uc;
 290                 if (Character.isSurrogate(c)) {
 291                     error = CoderResult.malformedForLength(len);
 292                     return -1;
 293                 }
 294                 if (dst.remaining() < 1) {
 295                     error = CoderResult.OVERFLOW;
 296                     return -1;
 297                 }
 298                 dst.put(c);
 299                 error = null;
 300                 return 1;
 301             } else if (Character.isValidCodePoint(uc)) {
 302                 if (dst.remaining() < 2) {
 303                     error = CoderResult.OVERFLOW;


 308                 error = null;
 309                 return 2;
 310             } else {
 311                 error = CoderResult.unmappableForLength(len);
 312                 return -1;
 313             }
 314         }
 315 
 316         /**
 317          * Generates one or two UTF-16 characters to represent the given UCS-4
 318          * character.
 319          *
 320          * @param  uc   The UCS-4 character
 321          * @param  len  The number of input bytes from which the UCS-4 value
 322          *              was constructed (used when creating result objects)
 323          * @param  da   The destination array, to which one or two UTF-16
 324          *              characters will be written
 325          * @param  dp   The destination position
 326          * @param  dl   The destination limit
 327          *
 328          * @returns  Either a positive count of the number of UTF-16 characters
 329          *           written to the destination buffer, or -1, in which case
 330          *           error() will return a descriptive result object
 331          */
 332         public int generate(int uc, int len, char[] da, int dp, int dl) {
 333             if (Character.isBmpCodePoint(uc)) {
 334                 char c = (char) uc;
 335                 if (Character.isSurrogate(c)) {
 336                     error = CoderResult.malformedForLength(len);
 337                     return -1;
 338                 }
 339                 if (dl - dp < 1) {
 340                     error = CoderResult.OVERFLOW;
 341                     return -1;
 342                 }
 343                 da[dp] = c;
 344                 error = null;
 345                 return 1;
 346             } else if (Character.isValidCodePoint(uc)) {
 347                 if (dl - dp < 2) {
 348                     error = CoderResult.OVERFLOW;


 158             return error;
 159         }
 160 
 161         /**
 162          * Returns an unmappable-input result object, with the appropriate
 163          * input length, for the previously-parsed character.
 164          */
 165         public CoderResult unmappableResult() {
 166             assert (error == null);
 167             return CoderResult.unmappableForLength(isPair ? 2 : 1);
 168         }
 169 
 170         /**
 171          * Parses a UCS-4 character from the given source buffer, handling
 172          * surrogates.
 173          *
 174          * @param  c    The first character
 175          * @param  in   The source buffer, from which one more character
 176          *              will be consumed if c is a high surrogate
 177          *
 178          * @return  Either a parsed UCS-4 character, in which case the isPair()
 179          *          and increment() methods will return meaningful values, or
 180          *          -1, in which case error() will return a descriptive result
 181          *          object
 182          */
 183         public int parse(char c, CharBuffer in) {
 184             if (Character.isHighSurrogate(c)) {
 185                 if (!in.hasRemaining()) {
 186                     error = CoderResult.UNDERFLOW;
 187                     return -1;
 188                 }
 189                 char d = in.get();
 190                 if (Character.isLowSurrogate(d)) {
 191                     character = Character.toCodePoint(c, d);
 192                     isPair = true;
 193                     error = null;
 194                     return character;
 195                 }
 196                 error = CoderResult.malformedForLength(1);
 197                 return -1;
 198             }
 199             if (Character.isLowSurrogate(c)) {
 200                 error = CoderResult.malformedForLength(1);
 201                 return -1;
 202             }
 203             character = c;
 204             isPair = false;
 205             error = null;
 206             return character;
 207         }
 208 
 209         /**
 210          * Parses a UCS-4 character from the given source buffer, handling
 211          * surrogates.
 212          *
 213          * @param  c    The first character
 214          * @param  ia   The input array, from which one more character
 215          *              will be consumed if c is a high surrogate
 216          * @param  ip   The input index
 217          * @param  il   The input limit
 218          *
 219          * @return  Either a parsed UCS-4 character, in which case the isPair()
 220          *          and increment() methods will return meaningful values, or
 221          *          -1, in which case error() will return a descriptive result
 222          *          object
 223          */
 224         public int parse(char c, char[] ia, int ip, int il) {
 225             assert (ia[ip] == c);
 226             if (Character.isHighSurrogate(c)) {
 227                 if (il - ip < 2) {
 228                     error = CoderResult.UNDERFLOW;
 229                     return -1;
 230                 }
 231                 char d = ia[ip + 1];
 232                 if (Character.isLowSurrogate(d)) {
 233                     character = Character.toCodePoint(c, d);
 234                     isPair = true;
 235                     error = null;
 236                     return character;
 237                 }
 238                 error = CoderResult.malformedForLength(1);
 239                 return -1;


 263 
 264         /**
 265          * If the previous generation operation detected an error, return the
 266          * object describing that error.
 267          */
 268         public CoderResult error() {
 269             assert error != null;
 270             return error;
 271         }
 272 
 273         /**
 274          * Generates one or two UTF-16 characters to represent the given UCS-4
 275          * character.
 276          *
 277          * @param  uc   The UCS-4 character
 278          * @param  len  The number of input bytes from which the UCS-4 value
 279          *              was constructed (used when creating result objects)
 280          * @param  dst  The destination buffer, to which one or two UTF-16
 281          *              characters will be written
 282          *
 283          * @return  Either a positive count of the number of UTF-16 characters
 284          *          written to the destination buffer, or -1, in which case
 285          *          error() will return a descriptive result object
 286          */
 287         public int generate(int uc, int len, CharBuffer dst) {
 288             if (Character.isBmpCodePoint(uc)) {
 289                 char c = (char) uc;
 290                 if (Character.isSurrogate(c)) {
 291                     error = CoderResult.malformedForLength(len);
 292                     return -1;
 293                 }
 294                 if (dst.remaining() < 1) {
 295                     error = CoderResult.OVERFLOW;
 296                     return -1;
 297                 }
 298                 dst.put(c);
 299                 error = null;
 300                 return 1;
 301             } else if (Character.isValidCodePoint(uc)) {
 302                 if (dst.remaining() < 2) {
 303                     error = CoderResult.OVERFLOW;


 308                 error = null;
 309                 return 2;
 310             } else {
 311                 error = CoderResult.unmappableForLength(len);
 312                 return -1;
 313             }
 314         }
 315 
 316         /**
 317          * Generates one or two UTF-16 characters to represent the given UCS-4
 318          * character.
 319          *
 320          * @param  uc   The UCS-4 character
 321          * @param  len  The number of input bytes from which the UCS-4 value
 322          *              was constructed (used when creating result objects)
 323          * @param  da   The destination array, to which one or two UTF-16
 324          *              characters will be written
 325          * @param  dp   The destination position
 326          * @param  dl   The destination limit
 327          *
 328          * @return  Either a positive count of the number of UTF-16 characters
 329          *          written to the destination buffer, or -1, in which case
 330          *          error() will return a descriptive result object
 331          */
 332         public int generate(int uc, int len, char[] da, int dp, int dl) {
 333             if (Character.isBmpCodePoint(uc)) {
 334                 char c = (char) uc;
 335                 if (Character.isSurrogate(c)) {
 336                     error = CoderResult.malformedForLength(len);
 337                     return -1;
 338                 }
 339                 if (dl - dp < 1) {
 340                     error = CoderResult.OVERFLOW;
 341                     return -1;
 342                 }
 343                 da[dp] = c;
 344                 error = null;
 345                 return 1;
 346             } else if (Character.isValidCodePoint(uc)) {
 347                 if (dl - dp < 2) {
 348                     error = CoderResult.OVERFLOW;
< prev index next >