Byte数组操作⽅法集(Java.Net)
在velocity优化时由于要将String转化为byte,所以就会涉及到⼀些针对byte数组的操作需要,如在⼀个数组中查⼀个⼩数组、数组替换、数组扩展等操作,下⾯这个类就提供了这样⼀组⽅法,⽽且性能还不错。
package com.taobao.sketch.util;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
/**
* ArrayUtil,⼀些基于byte数组的操作⽅法集
* <p/>
* Author By: junshan
* Created Date: 2010-12-27 16:17:23
*/
public class ArrayUtil {
/**
* 查并替换指定byte数组
*
* @param org of type byte[] 原数组
* @param search of type byte[] 要查的数组
* @param replace of type byte[] 要替换的数组
* @param startIndex of type int 开始搜索索引
* @return byte[] 返回新的数组
* @throws UnsupportedEncodingException when
*/
public static byte[] arrayReplace(byte[] org, byte[] search, byte[] replace, int startIndex) throws UnsupportedEncodingException {
int index = indexOf(org, search, startIndex);
if (index != -1) {
int newLength = org.length + replace.length – search.length;
byte[] newByte = new byte[newLength];
System.arraycopy(org, 0, newByte, 0, index);
System.arraycopy(replace, 0, newByte, index, replace.length);
//String newstr = new String(newByte, “GBK”);
//System.out.println(newstr);
if ((newByte.length – newStart) > replace.length) { return arrayReplace(newByte, search, replace, newStart); }
return newByte;
} else {
return org;
}
}
/**
* 从指定数组的copy⼀个⼦数组并返回
*
* @param org of type byte[] 原数组
* @param to 合并⼀个byte[]
* @return 合并的数据
*/
public static byte[] append(byte[] org, byte[] to) {
byte[] newByte = new byte[org.length + to.length]; System.arraycopy(org, 0, newByte, 0, org.length); System.arraycopy(to, 0, newByte, org.length, to.length); return newByte;
}
/**
* 从指定数组的copy⼀个⼦数组并返回
*
* @param org of type byte[] 原数组
* @param to 合并⼀个byte
* @return 合并的数据
*/
public static byte[] append(byte[] org, byte to) {
byte[] newByte = new byte[org.length + 1];
System.arraycopy(org, 0, newByte, 0, org.length); newByte[org.length] = to;
* 从指定数组的copy⼀个⼦数组并返回
*
* @param org of type byte[] 原数组
* @param from 起始点
* @param append 要合并的数据
*/
public static void append(byte[] org, int from, byte[] append) { System.arraycopy(append, 0, org, from, append.length);
}
/**
* 从指定数组的copy⼀个⼦数组并返回
*
* @param original of type byte[] 原数组
* @param from 起始点
* @param to 结束点
* @return 返回copy的数组
*/
public static byte[] copyOfRange(byte[] original, int from, int to) { int newLength = to – from;
if (newLength < 0)
throw new IllegalArgumentException(from + ” > ” + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length – from, newLength));
return copy;
}
public static byte[] char2byte(String encode, char… chars) { Charset cs = Charset.forName(encode);
CharBuffer cb = CharBuffer.allocate(chars.length);
cb.put(chars);
cb.flip();
ByteBuffer bb = cs.encode(cb);
* 查指定数组的起始索引
*
* @param org of type byte[] 原数组
* @param search of type byte[] 要查的数组
* @return int 返回索引
*/
public static int indexOf(byte[] org, byte[] search) {
java replace方法return indexOf(org, search, 0);
}
/**
* 查指定数组的起始索引
*
* @param org of type byte[] 原数组
* @param search of type byte[] 要查的数组
* @param startIndex 起始索引
* @return int 返回索引
*/
public static int indexOf(byte[] org, byte[] search, int startIndex) { KMPMatcher kmpMatcher = new com.taobao.sketch.util.ArrayUtil.KMPMatcher(); kmpMatcherputeFailure4Byte(search);
return kmpMatcher.indexOf(org, startIndex);
//return com.alibabamon.lang.ArrayUtil.indexOf(org, search);
}
/**
* 查指定数组的最后⼀次出现起始索引
*
* @param org of type byte[] 原数组
* @param search of type byte[] 要查的数组
* @return int 返回索引
*/
public static int lastIndexOf(byte[] org, byte[] search) {
return lastIndexOf(org, search, 0);
}
* @param org of type byte[] 原数组
* @param search of type byte[] 要查的数组
* @param fromIndex 起始索引
* @return int 返回索引
*/
public static int lastIndexOf(byte[] org, byte[] search, int fromIndex) { KMPMatcher kmpMatcher = new com.taobao.sketch.util.ArrayUtil.KMPMatcher(); kmpMatcherputeFailure4Byte(search);
return kmpMatcher.lastIndexOf(org, fromIndex);
}
/**
* KMP算法类
* <p/>
* Created on 2011-1-3
*/
static class KMPMatcher {
private int[] failure;
private int matchPoint;
private byte[] bytePattern;
/**
* Method indexOf …
*
* @param text of type byte[]
* @param startIndex of type int
* @return int
*/
public int indexOf(byte[] text, int startIndex) {
int j = 0;
if (text.length == 0 || startIndex > text.length) return -1;
for (int i = startIndex; i < text.length; i++) {
while (j > 0 && bytePattern[j] != text[i]) {
j = failure[j - 1];

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。