home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / jdbc / odbc / JdbcOdbcInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  3.4 KB  |  205 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.Statement;
  7.  
  8. public class JdbcOdbcInputStream extends InputStream {
  9.    protected JdbcOdbc OdbcApi;
  10.    protected int hStmt;
  11.    protected int column;
  12.    protected short type;
  13.    public static final short ASCII = 1;
  14.    public static final short UNICODE = 2;
  15.    public static final short BINARY = 3;
  16.    public static final short LOCAL = 4;
  17.    protected byte[] localByteArray;
  18.    protected int localOffset;
  19.    protected boolean invalid;
  20.    protected boolean highRead;
  21.    protected int sqlType;
  22.    protected byte[] buf;
  23.    public static final int MAX_BUF_LEN = 5120;
  24.    protected int convertType;
  25.    public static final int CONVERT_NONE = 0;
  26.    public static final int CONVERT_UNICODE = 1;
  27.    public static final int CONVERT_ASCII = 2;
  28.    public static final int CONVERT_BOTH = 3;
  29.    protected int convertMultiplier;
  30.    protected int bytesInBuf;
  31.    protected int bufOffset;
  32.    protected Statement ownerStatement;
  33.  
  34.    public JdbcOdbcInputStream(JdbcOdbc var1, int var2, int var3, short var4, int var5, Statement var6) {
  35.       this.OdbcApi = var1;
  36.       this.hStmt = var2;
  37.       this.column = var3;
  38.       this.type = var4;
  39.       this.invalid = false;
  40.       this.ownerStatement = var6;
  41.       this.sqlType = -2;
  42.       switch (var5) {
  43.          case -1:
  44.          case 1:
  45.          case 12:
  46.             this.sqlType = 1;
  47.          default:
  48.             this.convertMultiplier = 1;
  49.             this.convertType = 0;
  50.             switch (this.type) {
  51.                case 1:
  52.                   if (this.sqlType == -2) {
  53.                      this.convertMultiplier = 2;
  54.                      this.convertType = 2;
  55.                   }
  56.                   break;
  57.                case 2:
  58.                   if (this.sqlType == -2) {
  59.                      this.convertType = 3;
  60.                      this.convertMultiplier = 4;
  61.                   } else {
  62.                      this.convertType = 1;
  63.                      this.convertMultiplier = 2;
  64.                   }
  65.             }
  66.  
  67.             this.buf = new byte[5120 * this.convertMultiplier];
  68.             this.bytesInBuf = 0;
  69.             this.bufOffset = 0;
  70.       }
  71.    }
  72.  
  73.    public JdbcOdbcInputStream(JdbcOdbc var1, int var2, int var3, byte[] var4) {
  74.       this.OdbcApi = var1;
  75.       this.hStmt = var2;
  76.       this.column = var3;
  77.       this.type = 4;
  78.       this.localByteArray = var4;
  79.       this.localOffset = 0;
  80.       this.invalid = false;
  81.    }
  82.  
  83.    public int read() throws IOException {
  84.       byte[] var2 = new byte[1];
  85.       int var1 = this.read(var2);
  86.       if (var1 != -1) {
  87.          var1 = var2[0];
  88.       }
  89.  
  90.       return var1;
  91.    }
  92.  
  93.    public int read(byte[] var1) throws IOException {
  94.       if (var1.length == 0) {
  95.          return -1;
  96.       } else {
  97.          int var2 = 0;
  98.          if (this.invalid) {
  99.             throw new IOException("InputStream is no longer valid - the Statement has been closed, or the cursor has been moved");
  100.          } else {
  101.             switch (this.type) {
  102.                case 4:
  103.                   var2 = var1.length;
  104.                   if (this.localOffset + var2 > this.localByteArray.length) {
  105.                      var2 = this.localByteArray.length - this.localOffset;
  106.                   }
  107.  
  108.                   if (var2 == 0) {
  109.                      var2 = -1;
  110.                   } else {
  111.                      for(int var3 = 0; var3 < var2; ++var3) {
  112.                         var1[var3] = this.localByteArray[this.localOffset + var3];
  113.                      }
  114.  
  115.                      this.localOffset += var2;
  116.                   }
  117.                   break;
  118.                default:
  119.                   var2 = this.readData(var1);
  120.             }
  121.  
  122.             return var2;
  123.          }
  124.       }
  125.    }
  126.  
  127.    public int read(byte[] var1, int var2, int var3) throws IOException {
  128.       throw new IOException("Method not implemented");
  129.    }
  130.  
  131.    public int available() throws IOException {
  132.       throw new IOException();
  133.    }
  134.  
  135.    public void invalidate() {
  136.       this.invalid = true;
  137.    }
  138.  
  139.    protected int readData(byte[] var1) throws IOException {
  140.       int var2 = -1;
  141.       int var3 = 0;
  142.  
  143.       while(this.bytesInBuf != -1 && var3 < var1.length) {
  144.          if (this.bufOffset >= this.bytesInBuf) {
  145.             this.bytesInBuf = this.readBinaryData(this.buf, 5120);
  146.             this.bytesInBuf = this.convertData(this.buf, this.bytesInBuf);
  147.             this.bufOffset = 0;
  148.          } else {
  149.             var1[var3] = this.buf[this.bufOffset];
  150.             ++var3;
  151.             ++this.bufOffset;
  152.          }
  153.       }
  154.  
  155.       if (var3 > 0) {
  156.          var2 = var3;
  157.       }
  158.  
  159.       return var2;
  160.    }
  161.  
  162.    protected int readBinaryData(byte[] var1, int var2) throws IOException {
  163.       int var3 = 0;
  164.  
  165.       try {
  166.          var3 = this.OdbcApi.SQLGetDataBinary(this.hStmt, this.column, -2, var1, var2);
  167.       } catch (JdbcOdbcSQLWarning var6) {
  168.          Integer var5 = (Integer)var6.value;
  169.          var3 = var5;
  170.       } catch (SQLException var7) {
  171.          throw new IOException(((Throwable)var7).getMessage());
  172.       }
  173.  
  174.       return var3;
  175.    }
  176.  
  177.    protected int convertData(byte[] var1, int var2) {
  178.       if (this.convertType == 0) {
  179.          return var2;
  180.       } else {
  181.          String var3 = "0123456789ABCDEF";
  182.          if (var2 <= 0) {
  183.             return var2;
  184.          } else {
  185.             for(int var4 = var2 - 1; var4 >= 0; --var4) {
  186.                if (this.convertType == 3) {
  187.                   var1[var4 * 4 + 3] = (byte)var3.charAt(var1[var4] & 15);
  188.                   var1[var4 * 4 + 2] = 0;
  189.                   var1[var4 * 4 + 1] = (byte)var3.charAt(var1[var4] >> 4 & 15);
  190.                   var1[var4 * 4] = 0;
  191.                } else if (this.convertType == 2) {
  192.                   var1[var4 * 2 + 1] = (byte)var3.charAt(var1[var4] & 15);
  193.                   var1[var4 * 2] = (byte)var3.charAt(var1[var4] >> 4 & 15);
  194.                } else {
  195.                   var1[var4 * 2 + 1] = var1[var4];
  196.                   var1[var4 * 2] = 0;
  197.                }
  198.             }
  199.  
  200.             return var2 * this.convertMultiplier;
  201.          }
  202.       }
  203.    }
  204. }
  205.