<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    騎豬闖天下

    J2ME隨筆,記錄成長的腳步

    統(tǒng)計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    [JAVA]Utils類,java數(shù)據(jù)類型轉(zhuǎn)換小結(jié)

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

    開始今天的內(nèi)容,整理了部分Java數(shù)據(jù)類型的轉(zhuǎn)換,拿過來直接可以使用:
    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數(shù)據(jù)
         *
         * 
    @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;
            }

        }


        
    /**
         * 從字節(jié)數(shù)組中讀取一個Integer數(shù)據(jù)
         *
         * 
    @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;
            }

        }


        
    /**
         * 從字節(jié)數(shù)組中讀取一個Short數(shù)據(jù)
         *
         * 
    @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;
            }

        }


        
    /**
         * 從字節(jié)數(shù)組中讀取一個Long數(shù)據(jù)
         *
         * 
    @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的字節(jié)數(shù)組
         *
         * 
    @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數(shù)據(jù)
         *
         * 
    @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的字節(jié)數(shù)組
         *
         * 
    @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數(shù)據(jù)
         *
         * 
    @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的字節(jié)數(shù)組
         *
         * 
    @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 + "個字節(jié).");
                
    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 騎豬闖天下 閱讀(556) 評論(0)  編輯  收藏


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 色噜噜噜噜亚洲第一| 亚洲AV日韩精品久久久久| 黄页网站免费观看| 久久久久国色AV免费看图片| 可以免费观看一级毛片黄a| 久久久无码精品亚洲日韩软件 | 四虎永久免费网站免费观看| 国产AV无码专区亚洲AV手机麻豆| 亚洲图片一区二区| 国产精品亚洲专一区二区三区| 免费污视频在线观看| 最新猫咪www免费人成| 亚洲精品无码久久一线| 中文文字幕文字幕亚洲色| 一个人免费观看视频在线中文| 4455永久在线观免费看| 亚洲а∨天堂久久精品| 久久久久亚洲精品日久生情 | 国产精品偷伦视频观看免费| 亚洲av无码潮喷在线观看| 羞羞的视频在线免费观看| 中国人xxxxx69免费视频| 亚洲国产日韩在线观频| 亚洲熟妇AV日韩熟妇在线| 欧洲人成在线免费| 亚洲AⅤ无码一区二区三区在线 | 亚洲一区二区无码偷拍| 日韩免费毛片视频| 久久亚洲sm情趣捆绑调教| 国产1024精品视频专区免费| 久久精品国产亚洲av水果派| 国产精品免费大片| 亚洲视频无码高清在线| aⅴ在线免费观看| 亚洲av成人一区二区三区在线播放| 91福利免费体验区观看区| 亚洲精品少妇30p| 精品久久久久成人码免费动漫| 特黄aa级毛片免费视频播放| 久久亚洲AV无码精品色午夜麻| 成年女人喷潮毛片免费播放|