home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / DNSInput.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  2.8 KB  |  118 lines

  1. package org.xbill.DNS;
  2.  
  3. public class DNSInput {
  4.    private byte[] array;
  5.    private int pos;
  6.    private int end;
  7.    private int saved_pos;
  8.    private int saved_end;
  9.  
  10.    public DNSInput(byte[] input) {
  11.       this.array = input;
  12.       this.pos = 0;
  13.       this.end = this.array.length;
  14.       this.saved_pos = -1;
  15.       this.saved_end = -1;
  16.    }
  17.  
  18.    public int current() {
  19.       return this.pos;
  20.    }
  21.  
  22.    public int remaining() {
  23.       return this.end - this.pos;
  24.    }
  25.  
  26.    private void require(int n) throws WireParseException {
  27.       if (n > this.remaining()) {
  28.          throw new WireParseException("end of input");
  29.       }
  30.    }
  31.  
  32.    public void setActive(int len) {
  33.       if (len > this.array.length - this.pos) {
  34.          throw new IllegalArgumentException("cannot set active region past end of input");
  35.       } else {
  36.          this.end = this.pos + len;
  37.       }
  38.    }
  39.  
  40.    public void clearActive() {
  41.       this.end = this.array.length;
  42.    }
  43.  
  44.    public void jump(int index) {
  45.       if (index >= this.array.length) {
  46.          throw new IllegalArgumentException("cannot jump past end of input");
  47.       } else {
  48.          this.pos = index;
  49.          this.end = this.array.length;
  50.       }
  51.    }
  52.  
  53.    public void save() {
  54.       this.saved_pos = this.pos;
  55.       this.saved_end = this.end;
  56.    }
  57.  
  58.    public void restore() {
  59.       if (this.saved_pos < 0) {
  60.          throw new IllegalStateException("no previous state");
  61.       } else {
  62.          this.pos = this.saved_pos;
  63.          this.end = this.saved_end;
  64.          this.saved_pos = -1;
  65.          this.saved_end = -1;
  66.       }
  67.    }
  68.  
  69.    public int readU8() throws WireParseException {
  70.       this.require(1);
  71.       return this.array[this.pos++] & 255;
  72.    }
  73.  
  74.    public int readU16() throws WireParseException {
  75.       this.require(2);
  76.       int b1 = this.array[this.pos++] & 255;
  77.       int b2 = this.array[this.pos++] & 255;
  78.       return (b1 << 8) + b2;
  79.    }
  80.  
  81.    public long readU32() throws WireParseException {
  82.       this.require(4);
  83.       int b1 = this.array[this.pos++] & 255;
  84.       int b2 = this.array[this.pos++] & 255;
  85.       int b3 = this.array[this.pos++] & 255;
  86.       int b4 = this.array[this.pos++] & 255;
  87.       return ((long)b1 << 24) + (long)(b2 << 16) + (long)(b3 << 8) + (long)b4;
  88.    }
  89.  
  90.    public void readByteArray(byte[] b, int off, int len) throws WireParseException {
  91.       this.require(len);
  92.       System.arraycopy(this.array, this.pos, b, off, len);
  93.       this.pos += len;
  94.    }
  95.  
  96.    public byte[] readByteArray(int len) throws WireParseException {
  97.       this.require(len);
  98.       byte[] out = new byte[len];
  99.       System.arraycopy(this.array, this.pos, out, 0, len);
  100.       this.pos += len;
  101.       return out;
  102.    }
  103.  
  104.    public byte[] readByteArray() {
  105.       int len = this.remaining();
  106.       byte[] out = new byte[len];
  107.       System.arraycopy(this.array, this.pos, out, 0, len);
  108.       this.pos += len;
  109.       return out;
  110.    }
  111.  
  112.    public byte[] readCountedString() throws WireParseException {
  113.       this.require(1);
  114.       int len = this.array[this.pos++] & 255;
  115.       return this.readByteArray(len);
  116.    }
  117. }
  118.