src/java.base/share/classes/java/util/BitSet.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 50610 9b85066e259b
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    81      *
    81      *
    82      * The bits in this BitSet.  The ith bit is stored in bits[i/64] at
    82      * The bits in this BitSet.  The ith bit is stored in bits[i/64] at
    83      * bit position i % 64 (where bit position 0 refers to the least
    83      * bit position i % 64 (where bit position 0 refers to the least
    84      * significant bit and 63 refers to the most significant bit).
    84      * significant bit and 63 refers to the most significant bit).
    85      */
    85      */
       
    86     @java.io.Serial
    86     private static final ObjectStreamField[] serialPersistentFields = {
    87     private static final ObjectStreamField[] serialPersistentFields = {
    87         new ObjectStreamField("bits", long[].class),
    88         new ObjectStreamField("bits", long[].class),
    88     };
    89     };
    89 
    90 
    90     /**
    91     /**
   102      * the user knows what he's doing and try harder to preserve it.
   103      * the user knows what he's doing and try harder to preserve it.
   103      */
   104      */
   104     private transient boolean sizeIsSticky = false;
   105     private transient boolean sizeIsSticky = false;
   105 
   106 
   106     /* use serialVersionUID from JDK 1.0.2 for interoperability */
   107     /* use serialVersionUID from JDK 1.0.2 for interoperability */
       
   108     @java.io.Serial
   107     private static final long serialVersionUID = 7997698588986878753L;
   109     private static final long serialVersionUID = 7997698588986878753L;
   108 
   110 
   109     /**
   111     /**
   110      * Given a bit index, return word index containing it.
   112      * Given a bit index, return word index containing it.
   111      */
   113      */
  1122 
  1124 
  1123     /**
  1125     /**
  1124      * Save the state of the {@code BitSet} instance to a stream (i.e.,
  1126      * Save the state of the {@code BitSet} instance to a stream (i.e.,
  1125      * serialize it).
  1127      * serialize it).
  1126      */
  1128      */
       
  1129     @java.io.Serial
  1127     private void writeObject(ObjectOutputStream s)
  1130     private void writeObject(ObjectOutputStream s)
  1128         throws IOException {
  1131         throws IOException {
  1129 
  1132 
  1130         checkInvariants();
  1133         checkInvariants();
  1131 
  1134 
  1139 
  1142 
  1140     /**
  1143     /**
  1141      * Reconstitute the {@code BitSet} instance from a stream (i.e.,
  1144      * Reconstitute the {@code BitSet} instance from a stream (i.e.,
  1142      * deserialize it).
  1145      * deserialize it).
  1143      */
  1146      */
       
  1147     @java.io.Serial
  1144     private void readObject(ObjectInputStream s)
  1148     private void readObject(ObjectInputStream s)
  1145         throws IOException, ClassNotFoundException {
  1149         throws IOException, ClassNotFoundException {
  1146 
  1150 
  1147         ObjectInputStream.GetField fields = s.readFields();
  1151         ObjectInputStream.GetField fields = s.readFields();
  1148         words = (long[]) fields.get("bits", null);
  1152         words = (long[]) fields.get("bits", null);
  1180      * @return a string representation of this bit set
  1184      * @return a string representation of this bit set
  1181      */
  1185      */
  1182     public String toString() {
  1186     public String toString() {
  1183         checkInvariants();
  1187         checkInvariants();
  1184 
  1188 
       
  1189         final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8;
  1185         int numBits = (wordsInUse > 128) ?
  1190         int numBits = (wordsInUse > 128) ?
  1186             cardinality() : wordsInUse * BITS_PER_WORD;
  1191             cardinality() : wordsInUse * BITS_PER_WORD;
  1187         StringBuilder b = new StringBuilder(6*numBits + 2);
  1192         // Avoid overflow in the case of a humongous numBits
       
  1193         int initialCapacity = (numBits <= (MAX_INITIAL_CAPACITY - 2) / 6) ?
       
  1194             6 * numBits + 2 : MAX_INITIAL_CAPACITY;
       
  1195         StringBuilder b = new StringBuilder(initialCapacity);
  1188         b.append('{');
  1196         b.append('{');
  1189 
  1197 
  1190         int i = nextSetBit(0);
  1198         int i = nextSetBit(0);
  1191         if (i != -1) {
  1199         if (i != -1) {
  1192             b.append(i);
  1200             b.append(i);