< prev index next >

src/java.base/share/classes/java/lang/StringLatin1.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.util.Arrays;
  29 import java.util.Locale;
  30 import java.util.Objects;
  31 import java.util.Spliterator;
  32 import java.util.function.IntConsumer;
  33 import java.util.stream.IntStream;
  34 import jdk.internal.HotSpotIntrinsicCandidate;
  35 
  36 import static java.lang.String.LATIN1;
  37 import static java.lang.String.UTF16;
  38 import static java.lang.String.checkOffset;

  39 
  40 final class StringLatin1 {
  41 
  42     public static char charAt(byte[] value, int index) {
  43         if (index < 0 || index >= value.length) {
  44             throw new StringIndexOutOfBoundsException(index);
  45         }
  46         return (char)(value[index] & 0xff);
  47     }
  48 
  49     public static boolean canEncode(int cp) {
  50         return cp >>> 8 == 0;
  51     }
  52 
  53     public static int length(byte[] value) {
  54         return value.length;
  55     }
  56 
  57     public static int codePointAt(byte[] value, int index, int end) {
  58         return value[index] & 0xff;


 506     public static String newString(byte[] val, int index, int len) {
 507         return new String(Arrays.copyOfRange(val, index, index + len),
 508                           LATIN1);
 509     }
 510 
 511     public static void fillNull(byte[] val, int index, int end) {
 512         Arrays.fill(val, index, end, (byte)0);
 513     }
 514 
 515     // inflatedCopy byte[] -> char[]
 516     @HotSpotIntrinsicCandidate
 517     private static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
 518         for (int i = 0; i < len; i++) {
 519             dst[dstOff++] = (char)(src[srcOff++] & 0xff);
 520         }
 521     }
 522 
 523     // inflatedCopy byte[] -> byte[]
 524     @HotSpotIntrinsicCandidate
 525     public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {


 526         for (int i = 0; i < len; i++) {
 527             StringUTF16.putChar(dst, dstOff++, src[srcOff++] & 0xff);
 528         }
 529     }
 530 
 531     static class CharsSpliterator implements Spliterator.OfInt {
 532         private final byte[] array;
 533         private int index;        // current index, modified on advance/split
 534         private final int fence;  // one past last index
 535         private final int cs;
 536 
 537         CharsSpliterator(byte[] array, int acs) {
 538             this(array, 0, array.length, acs);
 539         }
 540 
 541         CharsSpliterator(byte[] array, int origin, int fence, int acs) {
 542             this.array = array;
 543             this.index = origin;
 544             this.fence = fence;
 545             this.cs = acs | Spliterator.ORDERED | Spliterator.SIZED


 566         }
 567 
 568         @Override
 569         public boolean tryAdvance(IntConsumer action) {
 570             if (action == null)
 571                 throw new NullPointerException();
 572             if (index >= 0 && index < fence) {
 573                 action.accept(array[index++] & 0xff);
 574                 return true;
 575             }
 576             return false;
 577         }
 578 
 579         @Override
 580         public long estimateSize() { return (long)(fence - index); }
 581 
 582         @Override
 583         public int characteristics() {
 584             return cs;
 585         }
 586     }
 587 
 588     ////////////////////////////////////////////////////////////////
 589 
 590     public static void getCharsSB(byte[] val, int srcBegin, int srcEnd, char dst[], int dstBegin) {
 591         checkOffset(srcEnd, val.length);
 592         getChars(val, srcBegin, srcEnd, dst, dstBegin);
 593     }
 594 
 595     public static void inflateSB(byte[] val, byte[] dst, int dstOff, int count) {
 596         checkOffset(count, val.length);
 597         checkOffset(dstOff + count, dst.length >> 1);  // dst is utf16
 598         inflate(val, 0, dst, dstOff, count);
 599     }
 600 }


  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.util.Arrays;
  29 import java.util.Locale;
  30 import java.util.Objects;
  31 import java.util.Spliterator;
  32 import java.util.function.IntConsumer;
  33 import java.util.stream.IntStream;
  34 import jdk.internal.HotSpotIntrinsicCandidate;
  35 
  36 import static java.lang.String.LATIN1;
  37 import static java.lang.String.UTF16;
  38 import static java.lang.String.checkOffset;
  39 import static java.lang.String.checkBoundsOffCount;
  40 
  41 final class StringLatin1 {
  42 
  43     public static char charAt(byte[] value, int index) {
  44         if (index < 0 || index >= value.length) {
  45             throw new StringIndexOutOfBoundsException(index);
  46         }
  47         return (char)(value[index] & 0xff);
  48     }
  49 
  50     public static boolean canEncode(int cp) {
  51         return cp >>> 8 == 0;
  52     }
  53 
  54     public static int length(byte[] value) {
  55         return value.length;
  56     }
  57 
  58     public static int codePointAt(byte[] value, int index, int end) {
  59         return value[index] & 0xff;


 507     public static String newString(byte[] val, int index, int len) {
 508         return new String(Arrays.copyOfRange(val, index, index + len),
 509                           LATIN1);
 510     }
 511 
 512     public static void fillNull(byte[] val, int index, int end) {
 513         Arrays.fill(val, index, end, (byte)0);
 514     }
 515 
 516     // inflatedCopy byte[] -> char[]
 517     @HotSpotIntrinsicCandidate
 518     private static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
 519         for (int i = 0; i < len; i++) {
 520             dst[dstOff++] = (char)(src[srcOff++] & 0xff);
 521         }
 522     }
 523 
 524     // inflatedCopy byte[] -> byte[]
 525     @HotSpotIntrinsicCandidate
 526     public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
 527         // We need a range check here because 'putChar' has no checks
 528         checkBoundsOffCount(dstOff, len, dst.length);
 529         for (int i = 0; i < len; i++) {
 530             StringUTF16.putChar(dst, dstOff++, src[srcOff++] & 0xff);
 531         }
 532     }
 533 
 534     static class CharsSpliterator implements Spliterator.OfInt {
 535         private final byte[] array;
 536         private int index;        // current index, modified on advance/split
 537         private final int fence;  // one past last index
 538         private final int cs;
 539 
 540         CharsSpliterator(byte[] array, int acs) {
 541             this(array, 0, array.length, acs);
 542         }
 543 
 544         CharsSpliterator(byte[] array, int origin, int fence, int acs) {
 545             this.array = array;
 546             this.index = origin;
 547             this.fence = fence;
 548             this.cs = acs | Spliterator.ORDERED | Spliterator.SIZED


 569         }
 570 
 571         @Override
 572         public boolean tryAdvance(IntConsumer action) {
 573             if (action == null)
 574                 throw new NullPointerException();
 575             if (index >= 0 && index < fence) {
 576                 action.accept(array[index++] & 0xff);
 577                 return true;
 578             }
 579             return false;
 580         }
 581 
 582         @Override
 583         public long estimateSize() { return (long)(fence - index); }
 584 
 585         @Override
 586         public int characteristics() {
 587             return cs;
 588         }













 589     }
 590 }
< prev index next >