home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / sun / misc / CharacterEncoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  2.6 KB  |  179 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. import java.nio.ByteBuffer;
  10.  
  11. public abstract class CharacterEncoder {
  12.    protected PrintStream pStream;
  13.  
  14.    protected abstract int bytesPerAtom();
  15.  
  16.    protected abstract int bytesPerLine();
  17.  
  18.    protected void encodeBufferPrefix(OutputStream var1) throws IOException {
  19.       this.pStream = new PrintStream(var1);
  20.    }
  21.  
  22.    protected void encodeBufferSuffix(OutputStream var1) throws IOException {
  23.    }
  24.  
  25.    protected void encodeLinePrefix(OutputStream var1, int var2) throws IOException {
  26.    }
  27.  
  28.    protected void encodeLineSuffix(OutputStream var1) throws IOException {
  29.       this.pStream.println();
  30.    }
  31.  
  32.    protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;
  33.  
  34.    protected int readFully(InputStream var1, byte[] var2) throws IOException {
  35.       for(int var3 = 0; var3 < var2.length; ++var3) {
  36.          int var4 = var1.read();
  37.          if (var4 == -1) {
  38.             return var3;
  39.          }
  40.  
  41.          var2[var3] = (byte)var4;
  42.       }
  43.  
  44.       return var2.length;
  45.    }
  46.  
  47.    public void encode(InputStream var1, OutputStream var2) throws IOException {
  48.       byte[] var5 = new byte[this.bytesPerLine()];
  49.       this.encodeBufferPrefix(var2);
  50.  
  51.       while(true) {
  52.          int var4 = this.readFully(var1, var5);
  53.          if (var4 == 0) {
  54.             break;
  55.          }
  56.  
  57.          this.encodeLinePrefix(var2, var4);
  58.  
  59.          for(int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) {
  60.             if (var3 + this.bytesPerAtom() <= var4) {
  61.                this.encodeAtom(var2, var5, var3, this.bytesPerAtom());
  62.             } else {
  63.                this.encodeAtom(var2, var5, var3, var4 - var3);
  64.             }
  65.          }
  66.  
  67.          if (var4 < this.bytesPerLine()) {
  68.             break;
  69.          }
  70.  
  71.          this.encodeLineSuffix(var2);
  72.       }
  73.  
  74.       this.encodeBufferSuffix(var2);
  75.    }
  76.  
  77.    public void encode(byte[] var1, OutputStream var2) throws IOException {
  78.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  79.       this.encode((InputStream)var3, var2);
  80.    }
  81.  
  82.    public String encode(byte[] var1) {
  83.       ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  84.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  85.       Object var4 = null;
  86.  
  87.       try {
  88.          this.encode((InputStream)var3, var2);
  89.          String var7 = var2.toString("8859_1");
  90.          return var7;
  91.       } catch (Exception var6) {
  92.          throw new Error("CharacterEncoder.encode internal error");
  93.       }
  94.    }
  95.  
  96.    private byte[] getBytes(ByteBuffer var1) {
  97.       byte[] var2 = null;
  98.       if (var1.hasArray()) {
  99.          byte[] var3 = var1.array();
  100.          if (var3.length == var1.capacity() && var3.length == var1.remaining()) {
  101.             var2 = var3;
  102.             var1.position(var1.limit());
  103.          }
  104.       }
  105.  
  106.       if (var2 == null) {
  107.          var2 = new byte[var1.remaining()];
  108.          var1.get(var2);
  109.       }
  110.  
  111.       return var2;
  112.    }
  113.  
  114.    public void encode(ByteBuffer var1, OutputStream var2) throws IOException {
  115.       byte[] var3 = this.getBytes(var1);
  116.       this.encode(var3, var2);
  117.    }
  118.  
  119.    public String encode(ByteBuffer var1) {
  120.       byte[] var2 = this.getBytes(var1);
  121.       return this.encode(var2);
  122.    }
  123.  
  124.    public void encodeBuffer(InputStream var1, OutputStream var2) throws IOException {
  125.       byte[] var5 = new byte[this.bytesPerLine()];
  126.       this.encodeBufferPrefix(var2);
  127.  
  128.       int var4;
  129.       do {
  130.          var4 = this.readFully(var1, var5);
  131.          if (var4 == 0) {
  132.             break;
  133.          }
  134.  
  135.          this.encodeLinePrefix(var2, var4);
  136.  
  137.          for(int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) {
  138.             if (var3 + this.bytesPerAtom() <= var4) {
  139.                this.encodeAtom(var2, var5, var3, this.bytesPerAtom());
  140.             } else {
  141.                this.encodeAtom(var2, var5, var3, var4 - var3);
  142.             }
  143.          }
  144.  
  145.          this.encodeLineSuffix(var2);
  146.       } while(var4 >= this.bytesPerLine());
  147.  
  148.       this.encodeBufferSuffix(var2);
  149.    }
  150.  
  151.    public void encodeBuffer(byte[] var1, OutputStream var2) throws IOException {
  152.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  153.       this.encodeBuffer((InputStream)var3, var2);
  154.    }
  155.  
  156.    public String encodeBuffer(byte[] var1) {
  157.       ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  158.       ByteArrayInputStream var3 = new ByteArrayInputStream(var1);
  159.  
  160.       try {
  161.          this.encodeBuffer((InputStream)var3, var2);
  162.       } catch (Exception var5) {
  163.          throw new Error("CharacterEncoder.encodeBuffer internal error");
  164.       }
  165.  
  166.       return var2.toString();
  167.    }
  168.  
  169.    public void encodeBuffer(ByteBuffer var1, OutputStream var2) throws IOException {
  170.       byte[] var3 = this.getBytes(var1);
  171.       this.encodeBuffer(var3, var2);
  172.    }
  173.  
  174.    public String encodeBuffer(ByteBuffer var1) {
  175.       byte[] var2 = this.getBytes(var1);
  176.       return this.encodeBuffer(var2);
  177.    }
  178. }
  179.