home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ldapjdk.jar / netscape / ldap / util / ByteBuf.class (.txt) next >
Encoding:
Java Class File  |  1999-04-13  |  3.1 KB  |  205 lines

  1. package netscape.ldap.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.RandomAccessFile;
  7.  
  8. public final class ByteBuf {
  9.    private byte[] value;
  10.    private int count;
  11.  
  12.    public ByteBuf() {
  13.       this(16);
  14.    }
  15.  
  16.    public ByteBuf(int var1) {
  17.       this.value = new byte[var1];
  18.    }
  19.  
  20.    public ByteBuf(String var1) {
  21.       this(var1.length() + 16);
  22.       this.append(var1);
  23.    }
  24.  
  25.    public ByteBuf(byte[] var1, int var2, int var3) {
  26.       this.value = new byte[var3];
  27.       System.arraycopy(var1, var2, this.value, 0, var3);
  28.       this.count = var3;
  29.    }
  30.  
  31.    public int length() {
  32.       return this.count;
  33.    }
  34.  
  35.    public int capacity() {
  36.       return this.value.length;
  37.    }
  38.  
  39.    public void ensureCapacity(int var1) {
  40.       int var2 = this.value.length;
  41.       if (var1 > var2) {
  42.          int var3 = (var2 + 1) * 2;
  43.          if (var1 > var3) {
  44.             var3 = var1;
  45.          }
  46.  
  47.          byte[] var4 = new byte[var3];
  48.          System.arraycopy(this.value, 0, var4, 0, this.count);
  49.          this.value = var4;
  50.       }
  51.  
  52.    }
  53.  
  54.    public void setLength(int var1) {
  55.       if (var1 < 0) {
  56.          throw new StringIndexOutOfBoundsException(var1);
  57.       } else {
  58.          if (this.count < var1) {
  59.             this.ensureCapacity(var1);
  60.  
  61.             while(this.count < var1) {
  62.                this.value[this.count] = 0;
  63.                ++this.count;
  64.             }
  65.          }
  66.  
  67.          this.count = var1;
  68.       }
  69.    }
  70.  
  71.    public byte byteAt(int var1) {
  72.       if (var1 >= 0 && var1 < this.count) {
  73.          return this.value[var1];
  74.       } else {
  75.          throw new StringIndexOutOfBoundsException(var1);
  76.       }
  77.    }
  78.  
  79.    public void getBytes(int var1, int var2, byte[] var3, int var4) {
  80.       if (var1 >= 0 && var1 < this.count) {
  81.          if (var2 >= 0 && var2 <= this.count) {
  82.             if (var1 < var2) {
  83.                System.arraycopy(this.value, var1, var3, var4, var2 - var1);
  84.             }
  85.  
  86.          } else {
  87.             throw new StringIndexOutOfBoundsException(var2);
  88.          }
  89.       } else {
  90.          throw new StringIndexOutOfBoundsException(var1);
  91.       }
  92.    }
  93.  
  94.    public void setByteAt(int var1, byte var2) {
  95.       if (var1 >= 0 && var1 < this.count) {
  96.          this.value[var1] = var2;
  97.       } else {
  98.          throw new StringIndexOutOfBoundsException(var1);
  99.       }
  100.    }
  101.  
  102.    public ByteBuf append(Object var1) {
  103.       return this.append(String.valueOf(var1));
  104.    }
  105.  
  106.    public ByteBuf append(String var1) {
  107.       if (var1 == null) {
  108.          var1 = String.valueOf(var1);
  109.       }
  110.  
  111.       int var2 = var1.length();
  112.       this.ensureCapacity(this.count + var2);
  113.  
  114.       for(int var3 = 0; var3 < var2; ++var3) {
  115.          this.value[this.count++] = (byte)var1.charAt(var3);
  116.       }
  117.  
  118.       return this;
  119.    }
  120.  
  121.    public ByteBuf append(byte[] var1) {
  122.       int var2 = var1.length;
  123.       this.ensureCapacity(this.count + var2);
  124.       System.arraycopy(var1, 0, this.value, this.count, var2);
  125.       this.count += var2;
  126.       return this;
  127.    }
  128.  
  129.    public ByteBuf append(byte[] var1, int var2, int var3) {
  130.       this.ensureCapacity(this.count + var3);
  131.       System.arraycopy(var1, var2, this.value, this.count, var3);
  132.       this.count += var3;
  133.       return this;
  134.    }
  135.  
  136.    public ByteBuf append(ByteBuf var1) {
  137.       this.append(var1.toBytes(), 0, var1.count);
  138.       return this;
  139.    }
  140.  
  141.    public ByteBuf append(boolean var1) {
  142.       return this.append(String.valueOf(var1));
  143.    }
  144.  
  145.    public ByteBuf append(byte var1) {
  146.       this.ensureCapacity(this.count + 1);
  147.       this.value[this.count++] = var1;
  148.       return this;
  149.    }
  150.  
  151.    public ByteBuf append(int var1) {
  152.       return this.append(String.valueOf(var1));
  153.    }
  154.  
  155.    public ByteBuf append(long var1) {
  156.       return this.append(String.valueOf(var1));
  157.    }
  158.  
  159.    public ByteBuf append(float var1) {
  160.       return this.append(String.valueOf(var1));
  161.    }
  162.  
  163.    public ByteBuf append(double var1) {
  164.       return this.append(String.valueOf(var1));
  165.    }
  166.  
  167.    public String toString() {
  168.       return new String(this.value, 0, this.count);
  169.    }
  170.  
  171.    public byte[] toBytes() {
  172.       byte[] var1 = new byte[this.count];
  173.       System.arraycopy(this.value, 0, var1, 0, this.count);
  174.       return var1;
  175.    }
  176.  
  177.    public int read(InputStream var1, int var2) throws IOException {
  178.       this.ensureCapacity(this.count + var2);
  179.       int var3 = var1.read(this.value, this.count, var2);
  180.       if (var3 > 0) {
  181.          this.count += var3;
  182.       }
  183.  
  184.       return var3;
  185.    }
  186.  
  187.    public int read(RandomAccessFile var1, int var2) throws IOException {
  188.       this.ensureCapacity(this.count + var2);
  189.       int var3 = var1.read(this.value, this.count, var2);
  190.       if (var3 > 0) {
  191.          this.count += var3;
  192.       }
  193.  
  194.       return var3;
  195.    }
  196.  
  197.    public void write(OutputStream var1) throws IOException {
  198.       var1.write(this.value, 0, this.count);
  199.    }
  200.  
  201.    public void write(RandomAccessFile var1) throws IOException {
  202.       var1.write(this.value, 0, this.count);
  203.    }
  204. }
  205.