home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &… the Search for Life CD 3 / 0_CD-ROM.iso / install / jre1_3 / lib / rt.jar / sun / jdbc / odbc / JdbcOdbcInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  3.6 KB  |  267 lines

  1. package sun.jdbc.odbc;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.sql.SQLException;
  6. import java.sql.SQLWarning;
  7. import java.sql.Statement;
  8.  
  9. public class JdbcOdbcInputStream extends InputStream {
  10.    protected JdbcOdbc OdbcApi;
  11.    protected int hStmt;
  12.    protected int column;
  13.    protected short type;
  14.    public static final short ASCII = 1;
  15.    public static final short UNICODE = 2;
  16.    public static final short BINARY = 3;
  17.    public static final short LOCAL = 4;
  18.    protected byte[] localByteArray;
  19.    protected int localOffset;
  20.    protected boolean invalid;
  21.    protected boolean highRead;
  22.    protected int sqlType;
  23.    protected byte[] buf;
  24.    public static final int MAX_BUF_LEN = 5120;
  25.    protected int convertType;
  26.    public static final int CONVERT_NONE = 0;
  27.    public static final int CONVERT_UNICODE = 1;
  28.    public static final int CONVERT_ASCII = 2;
  29.    public static final int CONVERT_BOTH = 3;
  30.    protected int convertMultiplier;
  31.    protected int bytesInBuf;
  32.    protected int bufOffset;
  33.    protected Statement ownerStatement;
  34.  
  35.    public JdbcOdbcInputStream(JdbcOdbc var1, int var2, int var3, short var4, int var5, Statement var6) {
  36.       this.OdbcApi = var1;
  37.       this.hStmt = var2;
  38.       this.column = var3;
  39.       this.type = var4;
  40.       this.invalid = false;
  41.       this.ownerStatement = var6;
  42.       this.sqlType = -2;
  43.       switch (var5) {
  44.          case -1:
  45.          case 1:
  46.          case 12:
  47.             this.sqlType = 1;
  48.          default:
  49.             this.convertMultiplier = 1;
  50.             this.convertType = 0;
  51.             switch (this.type) {
  52.                case 1:
  53.                   if (this.sqlType == -2) {
  54.                      this.convertMultiplier = 2;
  55.                      this.convertType = 2;
  56.                   }
  57.                   break;
  58.                case 2:
  59.                   if (this.sqlType == -2) {
  60.                      this.convertType = 3;
  61.                      this.convertMultiplier = 4;
  62.                   } else {
  63.                      this.convertType = 1;
  64.                      this.convertMultiplier = 2;
  65.                   }
  66.             }
  67.  
  68.             this.buf = new byte[5120 * this.convertMultiplier];
  69.             this.bytesInBuf = 0;
  70.             this.bufOffset = 0;
  71.       }
  72.    }
  73.  
  74.    public JdbcOdbcInputStream(JdbcOdbc var1, int var2, int var3, byte[] var4) {
  75.       this.OdbcApi = var1;
  76.       this.hStmt = var2;
  77.       this.column = var3;
  78.       this.type = 4;
  79.       this.localByteArray = var4;
  80.       this.localOffset = 0;
  81.       this.invalid = false;
  82.    }
  83.  
  84.    public int read() throws IOException {
  85.       byte[] var2 = new byte[1];
  86.       int var1 = this.read(var2);
  87.       if (var1 != -1) {
  88.          var1 = var2[0] & 255;
  89.       }
  90.  
  91.       return var1;
  92.    }
  93.  
  94.    public int read(byte[] var1) throws IOException {
  95.       return this.read(var1, 0, var1.length);
  96.    }
  97.  
  98.    public byte[] readAllData() throws IOException {
  99.       int var1 = 0;
  100.       if (this.invalid) {
  101.          throw new IOException("InputStream is no longer valid - the Statement has been closed, or the cursor has been moved");
  102.       } else {
  103.          byte[] var2;
  104.          switch (this.type) {
  105.             case 4:
  106.                if (this.localOffset + var1 > this.localByteArray.length) {
  107.                   var1 = this.localByteArray.length - this.localOffset;
  108.                }
  109.  
  110.                var2 = new byte[this.localByteArray.length];
  111.                if (var1 == 0) {
  112.                   var1 = -1;
  113.                } else {
  114.                   System.arraycopy(this.localByteArray, this.localOffset, var2, this.localOffset, var1);
  115.                   this.localOffset += var1;
  116.                }
  117.                break;
  118.             default:
  119.                var2 = this.readData();
  120.          }
  121.  
  122.          return var2;
  123.       }
  124.    }
  125.  
  126.    public int read(byte[] var1, int var2, int var3) throws IOException {
  127.       if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  128.          if (var3 == 0) {
  129.             return -1;
  130.          } else {
  131.             int var4 = 0;
  132.             if (this.invalid) {
  133.                throw new IOException("InputStream is no longer valid - the Statement has been closed, or the cursor has been moved");
  134.             } else {
  135.                switch (this.type) {
  136.                   case 4:
  137.                      var4 = var3;
  138.                      if (this.localOffset + var3 > this.localByteArray.length) {
  139.                         var4 = this.localByteArray.length - this.localOffset;
  140.                      }
  141.  
  142.                      if (var4 == 0) {
  143.                         var4 = -1;
  144.                      } else {
  145.                         for(int var5 = var2; var5 < var4; ++var5) {
  146.                            var1[var5] = this.localByteArray[this.localOffset + var5];
  147.                         }
  148.  
  149.                         this.localOffset += var4;
  150.                      }
  151.                      break;
  152.                   default:
  153.                      var4 = this.readData(var1, var2, var3);
  154.                }
  155.  
  156.                return var4;
  157.             }
  158.          }
  159.       } else {
  160.          throw new IndexOutOfBoundsException();
  161.       }
  162.    }
  163.  
  164.    public int available() throws IOException {
  165.       throw new IOException();
  166.    }
  167.  
  168.    public void invalidate() {
  169.       this.invalid = true;
  170.    }
  171.  
  172.    public byte[] readData() throws IOException {
  173.       byte[] var1 = null;
  174.       int var2 = 0;
  175.  
  176.       while(true) {
  177.          this.bytesInBuf = this.readBinaryData(this.buf, 5120);
  178.          this.bytesInBuf = this.convertData(this.buf, this.bytesInBuf);
  179.          if (this.bytesInBuf == -1) {
  180.             return var1;
  181.          }
  182.  
  183.          try {
  184.             if (var1 == null) {
  185.                var1 = new byte[this.bytesInBuf];
  186.             } else {
  187.                byte[] var3 = new byte[var2 + this.bytesInBuf];
  188.                System.arraycopy(var1, 0, var3, 0, var2);
  189.                var1 = var3;
  190.             }
  191.          } catch (OutOfMemoryError var4) {
  192.             ((JdbcOdbcStatement)this.ownerStatement).setWarning(new SQLWarning("Data has been truncated. " + ((Throwable)var4).getMessage()));
  193.             return var1;
  194.          }
  195.  
  196.          System.arraycopy(this.buf, 0, var1, var2, this.bytesInBuf);
  197.          var2 += this.bytesInBuf;
  198.       }
  199.    }
  200.  
  201.    protected int readData(byte[] var1, int var2, int var3) throws IOException {
  202.       int var4 = -1;
  203.       int var5 = var2;
  204.  
  205.       while(this.bytesInBuf != -1 && var5 < var3) {
  206.          if (this.bufOffset >= this.bytesInBuf) {
  207.             this.bytesInBuf = this.readBinaryData(this.buf, 5120);
  208.             this.bytesInBuf = this.convertData(this.buf, this.bytesInBuf);
  209.             this.bufOffset = 0;
  210.          } else {
  211.             var1[var5] = this.buf[this.bufOffset];
  212.             ++var5;
  213.             ++this.bufOffset;
  214.          }
  215.       }
  216.  
  217.       if (var5 > var2) {
  218.          var4 = var5;
  219.       }
  220.  
  221.       return var4;
  222.    }
  223.  
  224.    protected int readBinaryData(byte[] var1, int var2) throws IOException {
  225.       int var3 = 0;
  226.  
  227.       try {
  228.          var3 = this.OdbcApi.SQLGetDataBinary(this.hStmt, this.column, -2, var1, var2);
  229.       } catch (JdbcOdbcSQLWarning var6) {
  230.          Integer var5 = (Integer)var6.value;
  231.          var3 = var5;
  232.       } catch (SQLException var7) {
  233.          throw new IOException(((Throwable)var7).getMessage());
  234.       }
  235.  
  236.       return var3;
  237.    }
  238.  
  239.    protected int convertData(byte[] var1, int var2) {
  240.       if (this.convertType == 0) {
  241.          return var2;
  242.       } else {
  243.          String var3 = "0123456789ABCDEF";
  244.          if (var2 <= 0) {
  245.             return var2;
  246.          } else {
  247.             for(int var4 = var2 - 1; var4 >= 0; --var4) {
  248.                if (this.convertType == 3) {
  249.                   var1[var4 * 4 + 3] = (byte)var3.charAt(var1[var4] & 15);
  250.                   var1[var4 * 4 + 2] = 0;
  251.                   var1[var4 * 4 + 1] = (byte)var3.charAt(var1[var4] >> 4 & 15);
  252.                   var1[var4 * 4] = 0;
  253.                } else if (this.convertType == 2) {
  254.                   var1[var4 * 2 + 1] = (byte)var3.charAt(var1[var4] & 15);
  255.                   var1[var4 * 2] = (byte)var3.charAt(var1[var4] >> 4 & 15);
  256.                } else {
  257.                   var1[var4 * 2 + 1] = var1[var4];
  258.                   var1[var4 * 2] = 0;
  259.                }
  260.             }
  261.  
  262.             return var2 * this.convertMultiplier;
  263.          }
  264.       }
  265.    }
  266. }
  267.