home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / lang / StringBuffer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  5.6 KB  |  253 lines

  1. package java.lang;
  2.  
  3. public final class StringBuffer {
  4.    private char[] value;
  5.    private int count;
  6.    private boolean shared;
  7.  
  8.    public StringBuffer() {
  9.       this(16);
  10.    }
  11.  
  12.    public StringBuffer(int length) {
  13.       this.value = new char[length];
  14.       this.shared = false;
  15.    }
  16.  
  17.    public StringBuffer(String str) {
  18.       this(str.length() + 16);
  19.       this.append(str);
  20.    }
  21.  
  22.    public int length() {
  23.       return this.count;
  24.    }
  25.  
  26.    public int capacity() {
  27.       return this.value.length;
  28.    }
  29.  
  30.    private final void copyWhenShared() {
  31.       if (this.shared) {
  32.          char[] newValue = new char[this.value.length];
  33.          System.arraycopy(this.value, 0, newValue, 0, this.count);
  34.          this.value = newValue;
  35.          this.shared = false;
  36.       }
  37.  
  38.    }
  39.  
  40.    public synchronized void ensureCapacity(int minimumCapacity) {
  41.       int maxCapacity = this.value.length;
  42.       if (minimumCapacity > maxCapacity) {
  43.          int newCapacity = (maxCapacity + 1) * 2;
  44.          if (minimumCapacity > newCapacity) {
  45.             newCapacity = minimumCapacity;
  46.          }
  47.  
  48.          char[] newValue = new char[newCapacity];
  49.          System.arraycopy(this.value, 0, newValue, 0, this.count);
  50.          this.value = newValue;
  51.          this.shared = false;
  52.       }
  53.  
  54.    }
  55.  
  56.    public synchronized void setLength(int newLength) {
  57.       if (newLength < 0) {
  58.          throw new StringIndexOutOfBoundsException(newLength);
  59.       } else {
  60.          this.ensureCapacity(newLength);
  61.          if (this.count < newLength) {
  62.             this.copyWhenShared();
  63.  
  64.             while(this.count < newLength) {
  65.                this.value[this.count] = 0;
  66.                ++this.count;
  67.             }
  68.          }
  69.  
  70.          this.count = newLength;
  71.       }
  72.    }
  73.  
  74.    public synchronized char charAt(int index) {
  75.       if (index >= 0 && index < this.count) {
  76.          return this.value[index];
  77.       } else {
  78.          throw new StringIndexOutOfBoundsException(index);
  79.       }
  80.    }
  81.  
  82.    public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
  83.       if (srcBegin >= 0 && srcBegin < this.count) {
  84.          if (srcEnd >= 0 && srcEnd <= this.count) {
  85.             if (srcBegin < srcEnd) {
  86.                System.arraycopy(this.value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  87.             }
  88.  
  89.          } else {
  90.             throw new StringIndexOutOfBoundsException(srcEnd);
  91.          }
  92.       } else {
  93.          throw new StringIndexOutOfBoundsException(srcBegin);
  94.       }
  95.    }
  96.  
  97.    public synchronized void setCharAt(int index, char ch) {
  98.       if (index >= 0 && index < this.count) {
  99.          this.copyWhenShared();
  100.          this.value[index] = ch;
  101.       } else {
  102.          throw new StringIndexOutOfBoundsException(index);
  103.       }
  104.    }
  105.  
  106.    public synchronized StringBuffer append(Object obj) {
  107.       return this.append(String.valueOf(obj));
  108.    }
  109.  
  110.    public synchronized StringBuffer append(String str) {
  111.       if (str == null) {
  112.          str = String.valueOf(str);
  113.       }
  114.  
  115.       int len = str.length();
  116.       this.ensureCapacity(this.count + len);
  117.       this.copyWhenShared();
  118.       str.getChars(0, len, this.value, this.count);
  119.       this.count += len;
  120.       return this;
  121.    }
  122.  
  123.    public synchronized StringBuffer append(char[] str) {
  124.       int len = str.length;
  125.       this.ensureCapacity(this.count + len);
  126.       this.copyWhenShared();
  127.       System.arraycopy(str, 0, this.value, this.count, len);
  128.       this.count += len;
  129.       return this;
  130.    }
  131.  
  132.    public synchronized StringBuffer append(char[] str, int offset, int len) {
  133.       this.ensureCapacity(this.count + len);
  134.       this.copyWhenShared();
  135.       System.arraycopy(str, offset, this.value, this.count, len);
  136.       this.count += len;
  137.       return this;
  138.    }
  139.  
  140.    public StringBuffer append(boolean b) {
  141.       return this.append(String.valueOf(b));
  142.    }
  143.  
  144.    public synchronized StringBuffer append(char c) {
  145.       this.ensureCapacity(this.count + 1);
  146.       this.copyWhenShared();
  147.       this.value[this.count++] = c;
  148.       return this;
  149.    }
  150.  
  151.    public StringBuffer append(int i) {
  152.       return this.append(String.valueOf(i));
  153.    }
  154.  
  155.    public StringBuffer append(long l) {
  156.       return this.append(String.valueOf(l));
  157.    }
  158.  
  159.    public StringBuffer append(float f) {
  160.       return this.append(String.valueOf(f));
  161.    }
  162.  
  163.    public StringBuffer append(double d) {
  164.       return this.append(String.valueOf(d));
  165.    }
  166.  
  167.    public synchronized StringBuffer insert(int offset, Object obj) {
  168.       return this.insert(offset, String.valueOf(obj));
  169.    }
  170.  
  171.    public synchronized StringBuffer insert(int offset, String str) {
  172.       if (offset >= 0 && offset <= this.count) {
  173.          int len = str.length();
  174.          this.ensureCapacity(this.count + len);
  175.          this.copyWhenShared();
  176.          System.arraycopy(this.value, offset, this.value, offset + len, this.count - offset);
  177.          str.getChars(0, len, this.value, offset);
  178.          this.count += len;
  179.          return this;
  180.       } else {
  181.          throw new StringIndexOutOfBoundsException();
  182.       }
  183.    }
  184.  
  185.    public synchronized StringBuffer insert(int offset, char[] str) {
  186.       if (offset >= 0 && offset <= this.count) {
  187.          int len = str.length;
  188.          this.ensureCapacity(this.count + len);
  189.          this.copyWhenShared();
  190.          System.arraycopy(this.value, offset, this.value, offset + len, this.count - offset);
  191.          System.arraycopy(str, 0, this.value, offset, len);
  192.          this.count += len;
  193.          return this;
  194.       } else {
  195.          throw new StringIndexOutOfBoundsException();
  196.       }
  197.    }
  198.  
  199.    public StringBuffer insert(int offset, boolean b) {
  200.       return this.insert(offset, String.valueOf(b));
  201.    }
  202.  
  203.    public synchronized StringBuffer insert(int offset, char c) {
  204.       this.ensureCapacity(this.count + 1);
  205.       this.copyWhenShared();
  206.       System.arraycopy(this.value, offset, this.value, offset + 1, this.count - offset);
  207.       this.value[offset] = c;
  208.       ++this.count;
  209.       return this;
  210.    }
  211.  
  212.    public StringBuffer insert(int offset, int i) {
  213.       return this.insert(offset, String.valueOf(i));
  214.    }
  215.  
  216.    public StringBuffer insert(int offset, long l) {
  217.       return this.insert(offset, String.valueOf(l));
  218.    }
  219.  
  220.    public StringBuffer insert(int offset, float f) {
  221.       return this.insert(offset, String.valueOf(f));
  222.    }
  223.  
  224.    public StringBuffer insert(int offset, double d) {
  225.       return this.insert(offset, String.valueOf(d));
  226.    }
  227.  
  228.    public synchronized StringBuffer reverse() {
  229.       this.copyWhenShared();
  230.       int n = this.count - 1;
  231.  
  232.       for(int j = n - 1 >> 1; j >= 0; --j) {
  233.          char temp = this.value[j];
  234.          this.value[j] = this.value[n - j];
  235.          this.value[n - j] = temp;
  236.       }
  237.  
  238.       return this;
  239.    }
  240.  
  241.    public String toString() {
  242.       return new String(this);
  243.    }
  244.  
  245.    final void setShared() {
  246.       this.shared = true;
  247.    }
  248.  
  249.    final char[] getValue() {
  250.       return this.value;
  251.    }
  252. }
  253.