骑猪闯天下

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 骑猪闯天下 阅读(551) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: