騎豬闖天下

          J2ME隨筆,記錄成長的腳步

          統計

          留言簿(3)

          閱讀排行榜

          評論排行榜

          [JAVA]Utils類,java數據類型轉換小結

          2010年來了,為迎接虎年大吉,記住這個不平凡的虎年,今天開始寫東西!
          時至今日,J2ME方面的東西也有了一定的積累,好久不整理,有些東西都慢慢的開始生疏了,為了紀念曾經那些個日日夜夜的努力,在J2ME領域的不懈探索,本人決定,把當前所掌握的東東,分類整理出來,以此為曾經拼搏的我表示敬意!

          開始今天的內容,整理了部分Java數據類型的轉換,拿過來直接可以使用:
          import java.io.*;
          import java.net.SocketTimeoutException;

          /**
           * User: duchangfeng
           * Date: 2010-02-22
           * Time: 15:12:30
           
          */

          public class Utils {
              
          public static String appendToBinaryString(int size, String s) {
                  
          if (s == null) s = "";
                  
          for (int i = s.length(); i < size; i++{
                      s 
          = new StringBuilder().append("0").append(s).toString();
                  }

                  
          return s;
              }


              
          /**
               * 從InputStream中讀取一個Integer數據
               *
               * 
          @param is
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public synchronized int readInt(InputStream is) throws IOException {
                  
          try {
                      
          byte[] _byte = new byte[4];
                      
          synchronized (_byte) {
          //                readLargeSize(is,_byte,0);
                          is.read(_byte);
                          ByteArrayInputStream buf 
          = new ByteArrayInputStream(_byte);
                          DataInputStream in 
          = new DataInputStream(buf);
                          
          int readed = in.readInt();
                          
          //        System.out.println("read int "+readed);
                          if (readed > ConfigReader.getMaxPacketSize()) {
                              System.out.println(_byte);
                          }

                          
          return readed;
                      }

                  }
           catch (Exception e) {
          //            e.printStackTrace();
                      return (int0;
                  }

              }


              
          /**
               * 從字節數組中讀取一個Integer數據
               *
               * 
          @param bytes
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public synchronized int readInt(byte[] bytes) throws IOException {
                  
          try {
                      ByteArrayInputStream buf 
          = new ByteArrayInputStream(bytes);
                      DataInputStream in 
          = new DataInputStream(buf);
                      
          int readed = in.readInt();
                      
          //        System.out.println("read int "+readed);
                      return readed;
                  }
           catch (Exception e) {
                      e.printStackTrace();
                      
          return (int0;
                  }

              }


              
          /**
               * 從字節數組中讀取一個Short數據
               *
               * 
          @param bytes
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public synchronized short readShort(byte[] bytes) throws IOException {
                  
          try {
                      ByteArrayInputStream buf 
          = new ByteArrayInputStream(bytes);
                      DataInputStream in 
          = new DataInputStream(buf);
                      
          short readed = in.readShort();
                      
          //        System.out.println("read int "+readed);
                      return readed;
                  }
           catch (Exception e) {
                      e.printStackTrace();
                      
          return (int0;
                  }

              }


              
          /**
               * 從字節數組中讀取一個Long數據
               *
               * 
          @param bytes
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public synchronized long readLong(byte[] bytes) throws IOException {
                  
          try {
                      ByteArrayInputStream buf 
          = new ByteArrayInputStream(bytes);
                      DataInputStream in 
          = new DataInputStream(buf);
                      
          long readed = in.readLong();
                      
          //        System.out.println("read int "+readed);
                      return readed;
                  }
           catch (Exception e) {
                      e.printStackTrace();
                      
          return (int0;
                  }

              }


              
          /**
               * 獲取Integer的字節數組
               *
               * 
          @param target
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public byte[] getBytes(int target) throws IOException {
                  ByteArrayOutputStream buf 
          = new ByteArrayOutputStream();
                  DataOutputStream out 
          = new DataOutputStream(buf);
                  out.writeInt(target);
                  
          return buf.toByteArray();
              }


              
          /**
               * 從InputStream中讀取一個Short數據
               *
               * 
          @param is
               * 
          @return
               * 
          @throws IOException
               
          */

              
          public synchronized short readShort(InputStream is) throws IOException {
                  
          try {
                      
          byte[] _byte = new byte[2];
                      
          synchronized (_byte) {
          //                readLargeSize(is,_byte,0);
                          is.read(_byte);
                          ByteArrayInputStream buf 
          = new ByteArrayInputStream(_byte);
                          DataInputStream in 
          = new DataInputStream(buf);
                          
          short readed = in.readShort();
                          
          //        System.out.println("read short "+readed);
                          return readed;
                      }

                  }
           catch (Exception e) {
                      
          return (short0;
                  }

              }


              
          /**
               * 獲取Short的字節數組
               *
               * 
          @param target
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public byte[] getBytes(short target) throws IOException {
                  ByteArrayOutputStream buf 
          = new ByteArrayOutputStream();
                  DataOutputStream out 
          = new DataOutputStream(buf);
                  out.writeShort(target);
                  
          return buf.toByteArray();
              }


              
          /**
               * 從InputStream中讀取一個Long數據
               *
               * 
          @param is
               * 
          @return
               * 
          @throws IOException
               
          */

              
          public synchronized long readLong(InputStream is) throws IOException {
                  
          try {
                      
          byte[] _byte = new byte[8];
                      
          synchronized (_byte) {
                          is.read(_byte);
          //                readLargeSize(is,_byte,0);
                          ByteArrayInputStream buf = new ByteArrayInputStream(_byte);
                          DataInputStream in 
          = new DataInputStream(buf);
                          
          long readed = in.readLong();
                          
          //        System.out.println("read long "+readed);
                          return readed;
                      }

                  }
           catch (Exception e) {
                      
          return (long0;
                  }

              }


              
          /**
               * 獲取Long的字節數組
               *
               * 
          @param target
               * 
          @return
               * 
          @throws java.io.IOException
               
          */

              
          public byte[] getBytes(long target) throws IOException {
                  ByteArrayOutputStream buf 
          = new ByteArrayOutputStream();
                  DataOutputStream out 
          = new DataOutputStream(buf);
                  out.writeLong(target);
                  
          return buf.toByteArray();
              }


              
          public synchronized void readLargeSize(InputStream is, byte[] filePieces, int nowoffset) throws IOException, InterruptedException {
                  
          int read = 0;
                  
          try {
                      read 
          = is.read(filePieces, nowoffset, filePieces.length - nowoffset);
                      System.out.println(
          "讀取了" + read + "個字節.");
                      
          if (read != filePieces.length - nowoffset) {
                          readLargeSize(is, filePieces, nowoffset 
          + read);
                      }

                  }
           catch (SocketTimeoutException e) {
                      System.out.println(
          "read time out,nowoffset is " + (nowoffset + read));
                      e.printStackTrace();
                  }

              }

          }

          posted on 2010-02-22 15:38 騎豬闖天下 閱讀(560) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 城口县| 富顺县| 长丰县| 夏邑县| 陇川县| 平山县| 齐齐哈尔市| 应用必备| 平和县| 城固县| 镇坪县| 台北县| 正宁县| 视频| 女性| 乌鲁木齐县| 繁峙县| 观塘区| 郸城县| 金华市| 霸州市| 双流县| 肇源县| 澄江县| 增城市| 澜沧| 定南县| 大化| 固镇县| 方山县| 铜梁县| 察雅县| 包头市| 双城市| 夏邑县| 平原县| 陆丰市| 东阿县| 卢氏县| 凤阳县| 伊春市|