/*
Utils
Class for contining utility functions.
This file is part of JavaDBF packege.
author: anil@linuxense.com
license: LGPL (http://www.gnu.org/copyleft/lesser.html)
$Id: Utils.java,v 1.8 2004-07-19 08:58:11 anil Exp $
http://blog.csdn.net/wq1917/article/details/6139221
modifyed by lx 20120917 修改中文问题,及奇数缺少最后一个字节的问题。源代码为官方0.4.0最后的源码
*/
package com.linuxense.javadbf;
import java.io.*;
import java.util.*;
import java.text.*;
/**
Miscelaneous functions required by the JavaDBF package.
*/
public final class Utils {
public static final int ALIGN_LEFT = 10;
public static final int ALIGN_RIGHT = 12;
public static int readLittleEndianInt( DataInput in)
throws IOException {
int bigEndian = 0;
for( int shiftBy=0; shiftBy<32; shiftBy+=8) {
bigEndian |= (in.readUnsignedByte()&0xff) << shiftBy;
}
return bigEndian;
}
public static short readLittleEndianShort( DataInput in)
throws IOException {
int low = in.readUnsignedByte() & 0xff;
int high = in.readUnsignedByte();
return (short )(high << 8 | low);
}
public static byte[] trimLeftSpaces( byte [] arr) {
StringBuffer t_sb = new StringBuffer( arr.length);
for( int i=0; i<arr.length; i++) {
if( arr[i] != ' ') {
t_sb.append( (char)arr[ i]);
}
}
return t_sb.toString().getBytes();
}
public static short littleEndian( short value) {
short num1 = value;
short mask = (short)0xff;
short num2 = (short)(num1&mask);
num2<<=8;
mask<<=8;
num2 |= (num1&mask)>>8;
return num2;
}
public static int littleEndian(int value) {
int num1 = value;
int mask = 0xff;
int num2 = 0x00;
num2 |= num1 & mask;
for( int i=1; i<4; i++) {
num2<<=8;
mask <<= 8;
num2 |= (num1 & mask)>>(8*i);
}
return num2;
}
public static byte[] textPadding( String text, String characterSetName, int length) throws java.io.UnsupportedEncodingException {
return textPadding( text, characterSetName, length, Utils.ALIGN_LEFT);
}
public static byte[] textPadding( String text, String characterSetName, int length, int alignment) throws java.io.UnsupportedEncodingException {
return textPadding( text, characterSetName, length, alignment, (byte)' ');
}
public static byte[] textPadding( String text, String characterSetName, int length, int alignment,
byte paddingByte) throws java.io.UnsupportedEncodingException {
/* if( text.length() >= length) {
return text.substring( 0, length).getBytes( characterSetName);
}
byte byte_array[] = new byte[ length];
Arrays.fill( byte_array, paddingByte);
switch( alignment) {
case ALIGN_LEFT:
System.arraycopy( text.getBytes( characterSetName), 0, byte_array, 0, text.length());
break;
case ALIGN_RIGHT:
int t_offset = length - text.length();
System.arraycopy( text.getBytes( characterSetName), 0, byte_array, t_offset, text.length());
break;
}
return byte_array;*/
byte[] srcByteArray = text.getBytes(characterSetName);
byte[] byte_array = new byte[length];
Arrays.fill(byte_array, paddingByte);
int dstLength = 0;
if (srcByteArray.length > length) // 原先为if (srcByteArray.length >= length)
dstLength = (length % 2 == 0) ? length : length - 1;
else {
dstLength = srcByteArray.length;
}
switch (alignment)
{
case ALIGN_LEFT:
System.arraycopy(srcByteArray, 0, byte_array, 0, dstLength);
break;
case ALIGN_RIGHT:
int t_offset = length - dstLength;
System.arraycopy(srcByteArray, 0, byte_array,t_offset,
dstLength);
break;
}
return byte_array;
}
public static byte[] doubleFormating( Double doubleNum, String characterSetName, int fieldLength, int sizeDecimalPart) throws java.io.UnsupportedEncodingException{
int sizeWholePart = fieldLength - (sizeDecimalPart>0?( sizeDecimalPart + 1):0);
StringBuffer format = new StringBuffer( fieldLength);
for( int i=0; i<sizeWholePart; i++) {
format.append( "#");
}
if( sizeDecimalPart > 0) {
format.append( ".");
for( int i=0; i<sizeDecimalPart; i++) {
format.append( "0");
}
}
DecimalFormat df = new DecimalFormat( format.toString());
return textPadding( df.format( doubleNum.doubleValue()).toString(), characterSetName, fieldLength, ALIGN_RIGHT);
}
public static boolean contains( byte[] arr, byte value) {
boolean found = false;
for( int i=0; i<arr.length; i++) {
if( arr[i] == value) {
found = true;
break;
}
}
return found;
}
}
javadbf修正数据中文乱码及奇数字节丢失最后一个字节问题
5星 · 超过95%的资源 需积分: 50 133 浏览量
2012-09-18
09:37:56
上传
评论
收藏 18KB ZIP 举报
非洲小白脸最爱代劲
- 粉丝: 1
- 资源: 2
最新资源
- TFS(淘宝分布式存储引擎代码
- 海尔H42E10 6i981BTA DH1PY600000 MBOOT usb.bin 程序.zip
- 网盘的功能实现之数据库搭建笔记
- 软件工程与软件测试阶段作业三.doc
- 软件工程中的人工智能辅助设计研究.pptx
- 海尔智能电视刷机数据 H42E07 机编DH1QR000H07 务必确认机编一致 强制刷机 整机USB升级主程序
- 4444444444444444444
- 基于PHP实现响应式创业园区管委会网站.zip
- 安卓手机用作电脑网络摄像头的工具文件
- 海尔智能电视刷机数据 H32E10 机编DH1QX000805 务必确认机编一致 强制刷机 整机USB升级主程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页