栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java byte数组操作

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java byte数组操作

在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;

public class ArrayUtil {

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);

System.arraycopy(org, index + search.length, newByte, index + replace.length, org.length – index – search.length);

int newStart = 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;

}

}

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;

}

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;

return newByte;

}

public static void append(byte[] org, int from, byte[] append) {

System.arraycopy(append, 0, org, from, append.length);

}

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);

return bb.array();

}

public static int indexOf(byte[] org, byte[] search) {

return indexOf(org, search, 0);

}

public static int indexOf(byte[] org, byte[] search, int startIndex) {

KMPMatcher kmpMatcher = new com.taobao.sketch.util.ArrayUtil.KMPMatcher();

kmpMatcher.computeFailure4Byte(search);

return kmpMatcher.indexOf(org, startIndex);

//return com.alibaba.common.lang.ArrayUtil.indexOf(org, search);

}

public static int lastIndexOf(byte[] org, byte[] search) {

return lastIndexOf(org, search, 0);

}

public static int lastIndexOf(byte[] org, byte[] search, int fromIndex) {

KMPMatcher kmpMatcher = new com.taobao.sketch.util.ArrayUtil.KMPMatcher();

kmpMatcher.computeFailure4Byte(search);

return kmpMatcher.lastIndexOf(org, fromIndex);

}

static class KMPMatcher {

private int[] failure;

private int matchPoint;

private byte[] bytePattern;

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];

}

if (bytePattern[j] == text[i]) {

j++;

}

if (j == bytePattern.length) {

matchPoint = i – bytePattern.length + 1;

return matchPoint;

}

}

return -1;

}

public int lastIndexOf(byte[] text, int startIndex) {

matchPoint = -1;

int j = 0;

if (text.length == 0 || startIndex > text.length) return -1;

int end = text.length;

for (int i = startIndex; i < end; i++) {

while (j > 0 && bytePattern[j] != text[i]) {

j = failure[j - 1];

}

if (bytePattern[j] == text[i]) {

j++;

}

if (j == bytePattern.length) {

matchPoint = i – bytePattern.length + 1;

if ((text.length – i) > bytePattern.length) {

j = 0;

continue;

}

return matchPoint;

}

//如果从中间某个位置找,找到末尾没找到后,再重头开始找

if (startIndex != 0 && i + 1 == end) {

end = startIndex;

i = -1;

startIndex = 0;

}

}

return matchPoint;

}

public int lastIndexOfWithNoLoop(byte[] text, int startIndex) {

matchPoint = -1;

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];

}

if (bytePattern[j] == text[i]) {

j++;

}

if (j == bytePattern.length) {

matchPoint = i – bytePattern.length + 1;

if ((text.length – i) > bytePattern.length) {

j = 0;

continue;

}

return matchPoint;

}

}

return matchPoint;

}

public void computeFailure4Byte(byte[] patternStr) {

bytePattern = patternStr;

int j = 0;

int len = bytePattern.length;

failure = new int[len];

for (int i = 1; i < len; i++) {

while (j > 0 && bytePattern[j] != bytePattern[i]) {

j = failure[j - 1];

}

if (bytePattern[j] == bytePattern[i]) {

j++;

}

failure[i] = j;

}

}

}

public static void main(String[] args) {

try {

byte[] org = “kadeadedcfdededghkk”.getBytes(“GBK”);

byte[] search = “kk”.getBytes(“GBK”);

int last = lastIndexOf(org, search, 19);

long t1 = 0;

long t2 = 0;

int f1 = 0;

int f2 = 0;

for (int i = 0; i < 10000; i++) {

long s1 = System.nanoTime();

f1 = indexOf(org, search, 0);

long s2 = System.nanoTime();

f2 = com.alibaba.common.lang.ArrayUtil.indexOf(org, search);

long s3 = System.nanoTime();

t1 = t1 + (s2 – s1);

t2 = t2 + (s3 – s2);

}

System.out.println(“kmp=” + t1 / 10000 + “,ali=” + t2 / 10000);

System.out.printf(“f1=” + f1 + “,f2=” + f2);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

}

.net

在 stream流 和 byte[] 中查找(搜索)指定字符串

这里注重看的是两个 Search 的扩展方法,一个是 stream 类型的扩展,另一个是 byte[] 类型的扩展,

如果大家有更好的“算法”,请给回复,我们一起优化!

-- 常用扩展代码,需要这部分代码的支持!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Drawing;

namespace Ims.Bll

{

///

/// stream 、 string 、byte[] 间的转换扩展方法类

///

public static class StreamExtend

{

#region Stream 扩展

///

/// Stream Stream 转换为 byte 数组

///

///

public static byte[] ToByteArray(this Stream stream)

{

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

// 设置当前流的位置为流的开始

stream.Seek(0, SeekOrigin.Begin);

return bytes;

}

///

/// Stream 转换为 image 图片

///

///

public static Image ToImage(this Stream stream)

{

Image img = new Bitmap(stream);

return img;

}

///

/// Stream 转换为 string ,使用 Encoding.Default 编码

///

///

public static string ToStr(this Stream stream)

{

return System.Text.Encoding.Default.GetString(stream.ToByteArray());

}

///

/// 在当前流中搜索指定的 byte[]

///

///

/// 搜索关键字

/// 搜索开始位置

/// 如果存在则返回byte[]在流中首次出现的位置,否则返回 -1

public static long Search(this Stream stream, long beginPosition, byte[] key)

{

if (stream == null || stream.Length <= beginPosition)

return -1;

if (key == null || stream.Length < key.Length)

return -1;

long i=-1;

long j = -1;

int currentByte = int.MinValue;

for(i=beginPosition;i

{

if (stream.Length < key.Length + i)

break;

stream.Seek(i, SeekOrigin.Begin);

for (j = 0; j < key.Length; j++)

{

currentByte = stream.ReadByte();

if (currentByte != key[j])

break;

}

if (j == key.Length)

return i;

if(currentByte == -1)

break;

}

return -1;

}

#endregion

#region byte[] 扩展

///

/// byte[] 转换为 stream 流

///

///

public static Stream ToStream(this byte[] arr)

{

Stream stream = new MemoryStream(arr);

// 设置当前流的位置为流的开始 www.2cto.com

stream.Seek(0, SeekOrigin.Begin);

return stream;

}

///

/// byte[] 转换为 Image

///

///

public static Image ToImage(this byte[] arr)

{

return Image.FromStream(arr.ToStream());

}

///

/// 转换为 string,使用 Encoding.Default 编码

///

///

public static string ToStr(this byte[] arr)

{

return System.Text.Encoding.Default.GetString(arr);

}

///

/// 搜索

///

///

/// 搜索关键字

/// 搜索开始位置

///

public static int Search(this byte[] arr, int beginPosition, byte[] key)

{

if (arr == null || arr.Length <= beginPosition)

return -1;

if (key == null || arr.Length < key.Length)

return -1;

int i = -1;

int j = -1;

for (i = beginPosition; i < arr.Length; i++)

{

if (arr.Length < key.Length + i)

break;

for (j = 0; j < key.Length; j++)

{

if (arr[i+j] != key[j])

break;

}

if (j == key.Length)

return i;

}

return -1;

}

#endregion

#region string 扩展

///

/// string 转换为 byte[]

///

///

public static byte[] ToByteArray(this string str)

{

return System.Text.Encoding.Default.GetBytes(str);

}

///

/// string 转换为 Stream

///

///

public static Stream ToStream(this string str)

{

Stream stream = new MemoryStream(str.ToByteArray());

// 设置当前流的位置为流的开始

stream.Seek(0, SeekOrigin.Begin);

return stream;

}

#endregion

}

}

------------------------

-- 测试脚本

byte[] arr = "0123456789111".ToByteArray();

byte[] key1 = "123".ToByteArray();

byte[] key2 = "678".ToByteArray();

byte[] key3 = "911".ToByteArray();

byte[] key4 = "111".ToByteArray();

//流内搜索测试

Stream sm = arr.ToStream();

long index1 = sm.Search(0, key1);

long index2 = sm.Search(0, key2);

long index3 = sm.Search(0, key3);

long index4 = sm.Search(0, key4);

//byte[]内搜索测试

long index10 = arr.Search(0, key1);

long index20 = arr.Search(0, key2);

long index30 = arr.Search(0, key3);

long index40 = arr.Search(0, key4);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号