home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 31 / SUPERCDa.iso / Inet / HotJava / hjava.exe / Windows / resource / jre / lib / rt.jar / java / lang / StringBuffer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-11  |  4.8 KB  |  307 lines

  1. package java.lang;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.Serializable;
  6.  
  7. public final class StringBuffer implements Serializable {
  8.    private char[] value;
  9.    private int count;
  10.    private boolean shared;
  11.    static final long serialVersionUID = 3388685877147921107L;
  12.  
  13.    public StringBuffer() {
  14.       this(16);
  15.    }
  16.  
  17.    public StringBuffer(int var1) {
  18.       this.value = new char[var1];
  19.       this.shared = false;
  20.    }
  21.  
  22.    public StringBuffer(String var1) {
  23.       this(var1.length() + 16);
  24.       this.append(var1);
  25.    }
  26.  
  27.    public int length() {
  28.       return this.count;
  29.    }
  30.  
  31.    public int capacity() {
  32.       return this.value.length;
  33.    }
  34.  
  35.    private final void copy() {
  36.       char[] var1 = new char[this.value.length];
  37.       System.arraycopy(this.value, 0, var1, 0, this.count);
  38.       this.value = var1;
  39.       this.shared = false;
  40.    }
  41.  
  42.    public synchronized void ensureCapacity(int var1) {
  43.       if (var1 > this.value.length) {
  44.          this.expandCapacity(var1);
  45.       }
  46.  
  47.    }
  48.  
  49.    private void expandCapacity(int var1) {
  50.       int var2 = (this.value.length + 1) * 2;
  51.       if (var1 > var2) {
  52.          var2 = var1;
  53.       }
  54.  
  55.       char[] var3 = new char[var2];
  56.       System.arraycopy(this.value, 0, var3, 0, this.count);
  57.       this.value = var3;
  58.       this.shared = false;
  59.    }
  60.  
  61.    public synchronized void setLength(int var1) {
  62.       if (var1 < 0) {
  63.          throw new StringIndexOutOfBoundsException(var1);
  64.       } else {
  65.          if (var1 > this.value.length) {
  66.             this.expandCapacity(var1);
  67.          }
  68.  
  69.          if (this.count >= var1) {
  70.             this.count = var1;
  71.             if (this.shared) {
  72.                this.copy();
  73.             }
  74.  
  75.          } else {
  76.             if (this.shared) {
  77.                this.copy();
  78.             }
  79.  
  80.             while(this.count < var1) {
  81.                this.value[this.count] = 0;
  82.                ++this.count;
  83.             }
  84.  
  85.          }
  86.       }
  87.    }
  88.  
  89.    public synchronized char charAt(int var1) {
  90.       if (var1 >= 0 && var1 < this.count) {
  91.          return this.value[var1];
  92.       } else {
  93.          throw new StringIndexOutOfBoundsException(var1);
  94.       }
  95.    }
  96.  
  97.    public synchronized void getChars(int var1, int var2, char[] var3, int var4) {
  98.       if (var1 >= 0 && var1 < this.count) {
  99.          if (var2 >= 0 && var2 <= this.count) {
  100.             if (var1 < var2) {
  101.                System.arraycopy(this.value, var1, var3, var4, var2 - var1);
  102.             }
  103.  
  104.          } else {
  105.             throw new StringIndexOutOfBoundsException(var2);
  106.          }
  107.       } else {
  108.          throw new StringIndexOutOfBoundsException(var1);
  109.       }
  110.    }
  111.  
  112.    public synchronized void setCharAt(int var1, char var2) {
  113.       if (var1 >= 0 && var1 < this.count) {
  114.          if (this.shared) {
  115.             this.copy();
  116.          }
  117.  
  118.          this.value[var1] = var2;
  119.       } else {
  120.          throw new StringIndexOutOfBoundsException(var1);
  121.       }
  122.    }
  123.  
  124.    public synchronized StringBuffer append(Object var1) {
  125.       return this.append(String.valueOf(var1));
  126.    }
  127.  
  128.    public synchronized StringBuffer append(String var1) {
  129.       if (var1 == null) {
  130.          var1 = String.valueOf(var1);
  131.       }
  132.  
  133.       int var2 = var1.length();
  134.       int var3 = this.count + var2;
  135.       if (var3 > this.value.length) {
  136.          this.expandCapacity(var3);
  137.       }
  138.  
  139.       var1.getChars(0, var2, this.value, this.count);
  140.       this.count = var3;
  141.       return this;
  142.    }
  143.  
  144.    public synchronized StringBuffer append(char[] var1) {
  145.       int var2 = var1.length;
  146.       int var3 = this.count + var2;
  147.       if (var3 > this.value.length) {
  148.          this.expandCapacity(var3);
  149.       }
  150.  
  151.       System.arraycopy(var1, 0, this.value, this.count, var2);
  152.       this.count = var3;
  153.       return this;
  154.    }
  155.  
  156.    public synchronized StringBuffer append(char[] var1, int var2, int var3) {
  157.       int var4 = this.count + var3;
  158.       if (var4 > this.value.length) {
  159.          this.expandCapacity(var4);
  160.       }
  161.  
  162.       System.arraycopy(var1, var2, this.value, this.count, var3);
  163.       this.count = var4;
  164.       return this;
  165.    }
  166.  
  167.    public StringBuffer append(boolean var1) {
  168.       return this.append(String.valueOf(var1));
  169.    }
  170.  
  171.    public synchronized StringBuffer append(char var1) {
  172.       int var2 = this.count + 1;
  173.       if (var2 > this.value.length) {
  174.          this.expandCapacity(var2);
  175.       }
  176.  
  177.       this.value[this.count++] = var1;
  178.       return this;
  179.    }
  180.  
  181.    public StringBuffer append(int var1) {
  182.       return this.append(String.valueOf(var1));
  183.    }
  184.  
  185.    public StringBuffer append(long var1) {
  186.       return this.append(String.valueOf(var1));
  187.    }
  188.  
  189.    public StringBuffer append(float var1) {
  190.       return this.append(String.valueOf(var1));
  191.    }
  192.  
  193.    public StringBuffer append(double var1) {
  194.       return this.append(String.valueOf(var1));
  195.    }
  196.  
  197.    public synchronized StringBuffer insert(int var1, Object var2) {
  198.       return this.insert(var1, String.valueOf(var2));
  199.    }
  200.  
  201.    public synchronized StringBuffer insert(int var1, String var2) {
  202.       if (var1 >= 0 && var1 <= this.count) {
  203.          int var3 = var2.length();
  204.          int var4 = this.count + var3;
  205.          if (var4 > this.value.length) {
  206.             this.expandCapacity(var4);
  207.          } else if (this.shared) {
  208.             this.copy();
  209.          }
  210.  
  211.          System.arraycopy(this.value, var1, this.value, var1 + var3, this.count - var1);
  212.          var2.getChars(0, var3, this.value, var1);
  213.          this.count = var4;
  214.          return this;
  215.       } else {
  216.          throw new StringIndexOutOfBoundsException();
  217.       }
  218.    }
  219.  
  220.    public synchronized StringBuffer insert(int var1, char[] var2) {
  221.       if (var1 >= 0 && var1 <= this.count) {
  222.          int var3 = var2.length;
  223.          int var4 = this.count + var3;
  224.          if (var4 > this.value.length) {
  225.             this.expandCapacity(var4);
  226.          } else if (this.shared) {
  227.             this.copy();
  228.          }
  229.  
  230.          System.arraycopy(this.value, var1, this.value, var1 + var3, this.count - var1);
  231.          System.arraycopy(var2, 0, this.value, var1, var3);
  232.          this.count = var4;
  233.          return this;
  234.       } else {
  235.          throw new StringIndexOutOfBoundsException();
  236.       }
  237.    }
  238.  
  239.    public StringBuffer insert(int var1, boolean var2) {
  240.       return this.insert(var1, String.valueOf(var2));
  241.    }
  242.  
  243.    public synchronized StringBuffer insert(int var1, char var2) {
  244.       int var3 = this.count + 1;
  245.       if (var3 > this.value.length) {
  246.          this.expandCapacity(var3);
  247.       } else if (this.shared) {
  248.          this.copy();
  249.       }
  250.  
  251.       System.arraycopy(this.value, var1, this.value, var1 + 1, this.count - var1);
  252.       this.value[var1] = var2;
  253.       this.count = var3;
  254.       return this;
  255.    }
  256.  
  257.    public StringBuffer insert(int var1, int var2) {
  258.       return this.insert(var1, String.valueOf(var2));
  259.    }
  260.  
  261.    public StringBuffer insert(int var1, long var2) {
  262.       return this.insert(var1, String.valueOf(var2));
  263.    }
  264.  
  265.    public StringBuffer insert(int var1, float var2) {
  266.       return this.insert(var1, String.valueOf(var2));
  267.    }
  268.  
  269.    public StringBuffer insert(int var1, double var2) {
  270.       return this.insert(var1, String.valueOf(var2));
  271.    }
  272.  
  273.    public synchronized StringBuffer reverse() {
  274.       if (this.shared) {
  275.          this.copy();
  276.       }
  277.  
  278.       int var1 = this.count - 1;
  279.  
  280.       for(int var2 = var1 - 1 >> 1; var2 >= 0; --var2) {
  281.          char var3 = this.value[var2];
  282.          this.value[var2] = this.value[var1 - var2];
  283.          this.value[var1 - var2] = var3;
  284.       }
  285.  
  286.       return this;
  287.    }
  288.  
  289.    public String toString() {
  290.       return new String(this);
  291.    }
  292.  
  293.    final void setShared() {
  294.       this.shared = true;
  295.    }
  296.  
  297.    final char[] getValue() {
  298.       return this.value;
  299.    }
  300.  
  301.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  302.       var1.defaultReadObject();
  303.       this.value = (char[])this.value.clone();
  304.       this.shared = false;
  305.    }
  306. }
  307.