home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / misc / CharacterEncoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  2.3 KB  |  139 lines

  1. package sun.misc;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9.  
  10. public abstract class CharacterEncoder {
  11.    protected PrintStream pStream;
  12.  
  13.    protected abstract int bytesPerAtom();
  14.  
  15.    protected abstract int bytesPerLine();
  16.  
  17.    protected void encodeBufferPrefix(OutputStream var1) throws IOException {
  18.       this.pStream = new PrintStream(var1);
  19.    }
  20.  
  21.    protected void encodeBufferSuffix(OutputStream var1) throws IOException {
  22.    }
  23.  
  24.    protected void encodeLinePrefix(OutputStream var1, int var2) throws IOException {
  25.    }
  26.  
  27.    protected void encodeLineSuffix(OutputStream var1) throws IOException {
  28.       this.pStream.println();
  29.    }
  30.  
  31.    protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;
  32.  
  33.    protected int readFully(InputStream var1, byte[] var2) throws IOException {
  34.       for(int var3 = 0; var3 < var2.length; ++var3) {
  35.          int var4 = var1.read();
  36.          if (var4 == -1) {
  37.             return var3;
  38.          }
  39.  
  40.          var2[var3] = (byte)var4;
  41.       }
  42.  
  43.       return var2.length;
  44.    }
  45.  
  46.    public void encode(InputStream var1, OutputStream var2) throws IOException {
  47.       byte[] var5 = new byte[this.bytesPerLine()];
  48.       this.encodeBufferPrefix(var2);
  49.  
  50.       while(true) {
  51.          int var4 = this.readFully(var1, var5);
  52.          if (var4 == -1) {
  53.             break;
  54.          }
  55.  
  56.          this.encodeLinePrefix(var2, var4);
  57.  
  58.          for(int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) {
  59.             if (var3 + this.bytesPerAtom() <= var4) {
  60.                this.encodeAtom(var2, var5, var3, this.bytesPerAtom());
  61.             } else {
  62.                this.encodeAtom(var2, var5, var3, var4 - var3);
  63.             }
  64.          }
  65.  
  66.          if (var4 <= this.bytesPerLine()) {
  67.             break;
  68.          }
  69.  
  70.          this.encodeLineSuffix(var2);
  71.       }
  72.  
  73.       this.encodeBufferSuffix(var2);
  74.    }
  75.  
  76.    public void encode(byte[] var1, OutputStream var2) throws IOException {
  77.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  78.       this.encode((InputStream)var3, var2);
  79.    }
  80.  
  81.    public String encode(byte[] var1) {
  82.       ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  83.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  84.  
  85.       try {
  86.          this.encode((InputStream)var3, var2);
  87.       } catch (Exception var4) {
  88.          throw new Error("ChracterEncoder::encodeBuffer internal error");
  89.       }
  90.  
  91.       return var2.toString();
  92.    }
  93.  
  94.    public void encodeBuffer(InputStream var1, OutputStream var2) throws IOException {
  95.       byte[] var5 = new byte[this.bytesPerLine()];
  96.       this.encodeBufferPrefix(var2);
  97.  
  98.       int var4;
  99.       do {
  100.          var4 = this.readFully(var1, var5);
  101.          if (var4 == -1) {
  102.             break;
  103.          }
  104.  
  105.          this.encodeLinePrefix(var2, var4);
  106.  
  107.          for(int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) {
  108.             if (var3 + this.bytesPerAtom() <= var4) {
  109.                this.encodeAtom(var2, var5, var3, this.bytesPerAtom());
  110.             } else {
  111.                this.encodeAtom(var2, var5, var3, var4 - var3);
  112.             }
  113.          }
  114.  
  115.          this.encodeLineSuffix(var2);
  116.       } while(var4 >= this.bytesPerLine());
  117.  
  118.       this.encodeBufferSuffix(var2);
  119.    }
  120.  
  121.    public void encodeBuffer(byte[] var1, OutputStream var2) throws IOException {
  122.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  123.       this.encodeBuffer((InputStream)var3, var2);
  124.    }
  125.  
  126.    public String encodeBuffer(byte[] var1) {
  127.       ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  128.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  129.  
  130.       try {
  131.          this.encodeBuffer((InputStream)var3, var2);
  132.       } catch (Exception var4) {
  133.          throw new Error("ChracterEncoder::encodeBuffer internal error");
  134.       }
  135.  
  136.       return var2.toString();
  137.    }
  138. }
  139.