源码解读心得 - net.sf.hibernate.util.BytesHelper

该博客展示了Java中BytesHelper类的代码,此类封装了Array的相关操作,包含将byte数组转换为int、short,以及将int、short转换为byte数组的方法,还给出了测试代码,最后博主疑惑为何不用单元测试。


None.gif//$Id: BytesHelper.java,v 1.4 2003/06/15 12:45:08 oneovthafew Exp $
None.gif
package net.sf.hibernate.util;
ExpandedBlockStart.gifContractedBlock.gif
/**//**
InBlock.gif* 此类封装了Array的一些相关操作
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifContractedBlock.gif
public final class BytesHelper dot.gif{
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private BytesHelper() dot.gif{}
InBlock.gif    
InBlock.gif
InBlock.gif    
// 转换到int
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static int toInt( byte[] bytes ) dot.gif{
InBlock.gif        
int result = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i=0; i<4; i++dot.gif{
InBlock.gif            result 
= ( result << 8 ) - Byte.MIN_VALUE + (int) bytes[i];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif    
// 转换到short
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static short toShort( byte[] bytes ) dot.gif{
InBlock.gif        
return (short) ( ( ( - (short) Byte.MIN_VALUE + (short) bytes[0] ) << 8  ) - (short) Byte.MIN_VALUE + (short) bytes[1] );
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif    
// int转换到byte数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static byte[] toBytes(int value) dot.gif{
InBlock.gif        
byte[] result = new byte[4];
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i=3; i>=0; i--dot.gif{
InBlock.gif            result[i] 
= (byte) ( ( 0xFFl & value ) + Byte.MIN_VALUE );
InBlock.gif            value 
>>>= 8;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// short转换到byte数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static byte[] toBytes(short value) dot.gif{
InBlock.gif        
byte[] result = new byte[2];
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (int i=1; i>=0; i--dot.gif{
InBlock.gif            result[i] 
= (byte) ( ( 0xFFl & value )  + Byte.MIN_VALUE );
InBlock.gif            value 
>>>= 8;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif    
//以下为测试代码
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static void main( String[] args ) dot.gif{
InBlock.gif        System.
out.println( 0 +"=="+ BytesHelper.toInt( BytesHelper.toBytes(0) ) );
InBlock.gif        System.
out.println( 1 +"=="+ BytesHelper.toInt( BytesHelper.toBytes(1) ) );
InBlock.gif        System.
out.println( -1 +"=="+ BytesHelper.toInt( BytesHelper.toBytes(-1) ) );
InBlock.gif        System.
out.println( Integer.MIN_VALUE +"=="+ BytesHelper.toInt( BytesHelper.toBytes(Integer.MIN_VALUE) ) );
InBlock.gif        System.
out.println( Integer.MAX_VALUE +"=="+ BytesHelper.toInt( BytesHelper.toBytes(Integer.MAX_VALUE) ) );
InBlock.gif        System.
out.println( Integer.MIN_VALUE / 2 +"=="+ BytesHelper.toInt( BytesHelper.toBytes(Integer.MIN_VALUE / 2) ) );
InBlock.gif        System.
out.println( Integer.MAX_VALUE / 2 +"=="+ BytesHelper.toInt( BytesHelper.toBytes(Integer.MAX_VALUE / 2) ) );
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}


心得:
为什么测试不用单元测试呢,真是不明白。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值