<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Java究竟怎么玩?

      天地程序已定棋,人間大數待變局

        博客園  :: 首頁  :: 新隨筆  :: 聯系 :: 訂閱 訂閱  :: 管理
      準備先把以前寫的持久層及表示層框架寫完再寫loonframework-game包(實際上是想自己業余建站用,用現成的框架太無聊,重復發明輪子的最大意義就在于解悶……),在2005年時寫過一個開頭,由于自己沒有整理文檔,現在拿起來就覺得代碼很亂,又懶于寫文檔,于是把一些心得類的東西整理一下,用以備忘。

      在此持久層框架中,我將持久化過程分為兩個松耦合模塊,第一模塊封裝jdbc操作,隱藏Connection及相關事務,處理driver差異后執行標準CRUD并釋放資源,于第二模塊進行實體映射操作。

      但和Spring JdbcTemplate等jdbc封裝略有不同的是,我除了直接將結果集轉為List和實體返回外,還引入了一個類似CachedRowSet但非繼承CachedRowSet或ResultSet的結果集cache實體(沒有提供諸如CachedRowSet的execute功能,原因大家都知道……PS:05年我用Hibernate還比較少,現在看來和Hierbante的ScrollableResults接口超級類似,頗感java技術殊途同歸|||)。

      但在此類get數據時,由于我將所有ResultSet數據無差別以object方式存儲,當object為二進制對象時,為實現blob和clob接口就需要進行數據轉換,將二進制對象轉為blob或clob實現,為此完成代碼如下。

      比較hibernate的blobimpl而言(hibernate的blobimpl只有getBinaryStream()實現,因為別的對它也沒用……),實現了更多的函數以供調用。

      BlobImpl.java
      package org.loon.framework.dao.lob;

      import java.io.BufferedInputStream;
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.InputStream;

      import java.io.OutputStream;
      import java.sql.Blob;
      import java.sql.SQLException;

      import org.loon.framework.Loon;
      import org.loon.framework.helper.FileHelper;

      /**
       * <p>
       * Title: LoonFramework
       * </p>
       * <p>
       * Description:二進制大對象Blob實現,用于轉化二進制對象為blob實例,只提供get部分方法(雖然象征性的寫了
       * set實現,但沒有對數據庫進行操作,只是擺設……).
       * </p>
       * <p>
       * Copyright: Copyright (c) 2007
       * </p>
       * <p>
       * Company: LoonFramework
       * </p>
       * <p>
       * License: 
      http://www.apache.org/licenses/LICENSE-2.0
       * </p>
       * 
       * 
      @author chenpeng
       * @email:ceponline@yahoo.com.cn
       * 
      @version 0.1
       
      */

      public class BlobImpl implements Blob {


          
      private byte[] _bytes = new byte[0];

          
      private int _length = 0;

          
      /**
           * 構造函數,以byte[]構建blob
           * 
           * 
      @param bytes
           
      */

          
      public BlobImpl(byte[] bytes) {
              init(bytes);
          }


          
      /**
           * 構造函數,以InputStream構建blob
           * 
           * 
      @param bytes
           
      */

          
      public BlobImpl(InputStream input) {
              init(FileHelper.read(input));
          }


          
      /**
           * 構造函數,以blob重新構建blob
           * 
           * 
      @param bytes
           
      */

          
      public BlobImpl(Blob blob) {
              init(blobToBytes(blob));
          }


          
      /**
           * 初始化byte[]
           * 
           * 
      @param b
           
      */

          
      private void init(byte[] bytes) {
              _bytes 
      = bytes;
              _length 
      = _bytes.length;
          }


          
      /**
           * 將blob轉為byte[]
           * 
           * 
      @param blob
           * 
      @return
           
      */

          
      private byte[] blobToBytes(Blob blob) {
              BufferedInputStream is 
      = null;
              
      try {
                  is 
      = new BufferedInputStream(blob.getBinaryStream());
                  
      byte[] bytes = new byte[(int) blob.length()];
                  
      int len = bytes.length;
                  
      int offset = 0;
                  
      int read = 0;
                  
      while (offset < len
                          
      && (read = is.read(bytes, offset, len - offset)) >= 0{
                      offset 
      += read;
                  }

                  
      return bytes;
              }
       catch (Exception e) {
                  
      return null;
              }
       finally {
                  
      try {
                      is.close();
                      is 
      = null;
                  }
       catch (IOException e) {
                      
      return null;
                  }


              }

          }


          
      /**
           * 獲得blob中數據實際長度
           * 
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public long length() throws SQLException {
              
      return _bytes.length;
          }


          
      /**
           * 返回指定長度的byte[]
           * 
           * 
      @param pos
           * 
      @param len
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public byte[] getBytes(long pos, int len) throws SQLException {
              
      if (pos == 0 && len == length())
                  
      return _bytes;
              
      try {
                  
      byte[] newbytes = new byte[len];
                  System.arraycopy(_bytes, (
      int) pos, newbytes, 0, len);
                  
      return newbytes;
              }
       catch (Exception e) {
                  
      throw new SQLException("Inoperable scope of this array");
              }

          }


          
      /**
           * 返回InputStream
           * 
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public InputStream getBinaryStream() throws SQLException {
              
      return new ByteArrayInputStream(_bytes);
          }


          
      /**
           * 獲取此byte[]中start的字節位置
           * 
           * 
      @param pattern
           * 
      @param start
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public long position(byte[] pattern, long start) throws SQLException {
              start
      --;
              
      if (start < 0{
                  
      throw new SQLException("start < 0");
              }

              
      if (start >= _length) {
                  
      throw new SQLException("start >= max length");
              }

              
      if (pattern == null{
                  
      throw new SQLException("pattern == null");
              }

              
      if (pattern.length == 0 || _length == 0 || pattern.length > _length) {
                  
      return -1;
              }

              
      int limit = (int) _length - pattern.length;
              
      for (int i = (int) start; i <= limit; i++{
                  
      int p;
                  
      for (p = 0; p < pattern.length && _bytes[i + p] == pattern[p]; p++{
                      
      if (p == pattern.length) {
                          
      return i + 1;
                      }

                  }

              }

              
      return -1;
          }


          
      /**
           * 獲取指定的blob中start的字節位置
           * 
           * 
      @param pattern
           * 
      @param start
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public long position(Blob pattern, long start) throws SQLException {
              
      return position(blobToBytes(pattern), start);
          }


          
      /**
           * 不支持操作異常拋出
           * 
           
      */

          
      void nonsupport() {
              
      throw new UnsupportedOperationException("This method is not supported!");
          }


          
      /**
           * 釋放Blob對象資源
           * 
           * 
      @throws SQLException
           
      */

          
      public void free() throws SQLException {
              _bytes 
      = new byte[0];
              _length 
      = 0;
          }


          
      /**
           * 返回指定長度部分的InputStream,并返回InputStream
           * 
           * 
      @param pos
           * 
      @param len
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public InputStream getBinaryStream(long pos, long len) throws SQLException {
              
      return new ByteArrayInputStream(getBytes(pos, (int) len));
          }


          
      /**
           * 以指定指定長度將二進制流寫入OutputStream,并返回OutputStream
           * 
           * 
      @param pos
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public OutputStream setBinaryStream(long pos) throws SQLException {
              
      // 暫不支持
              nonsupport();
              pos
      --;
              
      if (pos < 0{
                  
      throw new SQLException("pos < 0");
              }

              
      if (pos > _length) {
                  
      throw new SQLException("pos > length");
              }

              
      // 將byte[]轉為ByteArrayInputStream
              ByteArrayInputStream inputStream = new ByteArrayInputStream(_bytes);
              ByteArrayOutputStream os 
      = new ByteArrayOutputStream();
              
      byte[] bytes = new byte[(int) pos];
              
      try {
                  bytes 
      = new byte[inputStream.available()];
                  
      int read;
                  
      while ((read = inputStream.read(bytes)) >= 0{
                      os.write(bytes, 
      0, read);
                  }


              }
       catch (IOException e) {
                  e.getStackTrace();
              }


              
      // 返回OutputStream
              return (OutputStream) os;
          }


          
      /**
           * 設定byte[]
           * 
           * 
      @param pos
           * 
      @param bytes
           * 
      @param offset
           * 
      @param size
           * 
      @param copy
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      private int setBytes(long pos, byte[] bytes, int offset, int size,
                  
      boolean copy) throws SQLException {
              
      // 暫不支持
              nonsupport();
              pos
      --;
              
      if (pos < 0{
                  
      throw new SQLException("pos < 0");
              }

              
      if (pos > _length) {
                  
      throw new SQLException("pos > max length");
              }

              
      if (bytes == null{
                  
      throw new SQLException("bytes == null");
              }

              
      if (offset < 0 || offset > bytes.length) {
                  
      throw new SQLException("offset < 0 || offset > bytes.length");
              }

              
      if (size < 0 || pos + size > (long) Integer.MAX_VALUE
                      
      || offset + size > bytes.length) {
                  
      throw new SQLException();
              }

              
      // 當copy數據時
              if (copy) {
                  _bytes 
      = new byte[size];
                  System.arraycopy(bytes, offset, _bytes, 
      0, size);
              }
       else // 否則直接替換對象
                  _bytes = bytes;
              }

              
      return _bytes.length;
          }


          
      /**
           * 設定指定開始位置byte[]
           * 
           * 
      @param pos
           * 
      @param bytes
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public int setBytes(long pos, byte[] bytes) throws SQLException {
              
      // 暫不支持
              nonsupport();
              
      return setBytes(pos, bytes, 0, bytes.length, true);
          }


          
      /**
           * 設定byte[]
           * 
           * 
      @param pos
           * 
      @param bytes
           * 
      @param offset
           * 
      @param len
           * 
      @return
           * 
      @throws SQLException
           
      */

          
      public int setBytes(long pos, byte[] bytes, int offset, int len)
                  
      throws SQLException {
              
      // 暫不支持
              nonsupport();
              
      return setBytes(pos, bytes, offset, len, true);
          }


          
      /**
           * 截取相應部分數據
           * 
           * 
      @param len
           * 
      @throws SQLException
           
      */

          
      public void truncate(long len) throws SQLException {
              
      if (len < 0{
                  
      throw new SQLException("len < 0");
              }

              
      if (len > _length) {
                  
      throw new SQLException("len > max length");
              }

              _length 
      = (int) len;
          }


          
      public static void main(String[] args) {

              
      //獲得一個指定文件的blob
              
      //PS:天地良心啊,沒抄襲hibernate寫法,無奈的寫一樣了,不過比他多實現了點方法……(還特意把函數改名,不然更像|||)……
              Blob blob = Loon.makeBlob("D:/test.txt"); 
              
      // 以byte[]獲得blob實例
              
      //Blob blob = new BlobImpl(bytes);
              try {
                  
      // getBytes測試
                  
      // 取出0到blob結束范圍的byte[]
                  byte[] buffer = blob.getBytes(0, (int) blob.length());
                  
      // 以gb2312編碼將byte[]轉為string顯示
                  System.out.println(new String(buffer, "gb2312"));

                  
      // getBinaryStream測試
                  BufferedInputStream is = new BufferedInputStream(blob
                          .getBinaryStream());
                  buffer 
      = new byte[(int) blob.length()];
                  
      int len = buffer.length;
                  
      int offset = 0;
                  
      int read = 0;
                  
      while (offset < len
                          
      && (read = is.read(buffer, offset, len - offset)) >= 0{
                      offset 
      += read;
                  }

                  System.out.println(
      new String(buffer, "gb2312"));

                  
      // getBinaryStream范圍取值測試,取0to4的byte[]
                  is = new BufferedInputStream(blob.getBinaryStream(04));
                  
      //將is轉為byte[]后轉為String顯示
                  System.out.println(FileHelper.readToString(is, "gb2312"));

              }
       catch (Exception e) {
                  e.printStackTrace();
              }


          }


      }

      posted on 2008-02-17 13:07  cping  閱讀(1362)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 永久免费AV无码网站大全| 亚洲成a人v欧美综合天堂下载| 精品人妻中文字幕av| 国产福利片无码区在线观看| 漳州市| 成人区人妻精品一区二区| 亚洲综合精品成人| 久热这里只精品视频99| 在线 国产 欧美 专区| 永久无码天堂网小说区| 国产成人AV国语在线观看| 亚洲国产精品男人的天堂| CAOPORN免费视频国产| 亚洲国产精品无码观看久久| 欧美猛少妇色xxxxx猛叫| 无人区码一码二码三码区| 国产一区日韩二区三区| 国产精品一区二区国产馆| 日韩在线观看精品亚洲| 日韩人妻中文字幕精品| 麻豆人人妻人人妻人人片av| A级日本乱理伦片免费入口| 四虎库影成人在线播放| 亚洲男人天堂2018| 午夜福利日本一区二区无码| 亚洲精品第一国产综合精品| 无码高潮爽到爆的喷水视频| 狼人大伊人久久一区二区| 国产精品老熟女露脸视频| 97一区二区国产好的精华液| 日韩精品亚洲 国产| 国内久久人妻风流av免费| 年轻女教师hd中字3| 成人亚欧欧美激情在线观看| 少妇人妻偷人精品免费视频| 久久天天躁夜夜躁狠狠综合| 被拉到野外强要好爽| 亚洲V天堂V手机在线| 中文字幕在线视频不卡一区二区| 好紧好湿好黄的视频| 国产精品无卡毛片视频|