Scene:
1. C + + can use std::string to cache uint8_t byte arrays, for example, when receiving a socket packet, need to receive the complete before processing some data, it is necessary to cache and then processing.
2. The problem is that Java string is a stored Unicode double-byte structure that supports only characters, does not support such characters, and is not suitable for processing byte data.
Let's look at what classes Java provides for byte processing, but none of them have the ability to temporarily cache.
1. Java.lang.Byte class: Handles conversions between string and byte types, including support cardinality. Unfortunately, the maximum size conversion of byte 1 bytes is supported. 2. The arraycopy of the Java.lang.System class handles copying between arrays, the original type is the value copy, and for the object type is called the Clone Method (estimate). static void Arraycopy (object src, int srcpos, Object DST, int dstpos, int length) Copies length elements from the array src, starting @ offset Srcpos, into the array DS T, starting at offset Dstpos. 3. Java.util.Arrays class static int BinarySearch (byte[] array, byte value) performs a binary search for value in the ascending Sorted array Array. Static byte[] CopyOf (byte[] original, int newlength) Copies newlength elements from original to a new array. static void sort (byte[] array) static string toString (byte[] array) Creates a String representation of the byte[] passed. static void Fill (byte[] array, byte value) Fills the specified array with the specified element. Static Boolean equals (Byte[] array1, byte[] array2) compares the arrays. Static byte[] Copyofrange (byte[] original, int start, int end) Copies elements from original to a new array, from Indexes start (inclusive) to end (exclusive). Static <T> list<t> aslist (T ... array) Returns a List of the objects in the specified array.
Scheme:
1. Encapsulate a byte[] array of processing classes, refer to std::string.
2. Temporary program, time relationship is not optimized, here also gave a basic idea, have the ability to expand it yourself, such as Std::string's find.
Package Com.androidassist.util;import Java.util.arrays;public class Byteutils {private int size;private byte[] buffer; Public byte[] GetBuffer () {return buffer;} Private final int kbuffersizeincrease = 512;private final int kdefaultbuffersize = 1024;public byteutils () {buffer = new B yte[kdefaultbuffersize];size = 0;} Public long GetSize () {return size;} public void setSize (int size) {this.size = size;} Public byteutils append (byte[] buf, int length) {if (size + length > buffer.length) {buffer = arrays.copyof (buffer, buf Fer.length + kbuffersizeincrease);} System.arraycopy (buf, 0, buffer, size, length); Size + = Length;return this;} public void erase (int begin, int count) {if (begin + Count > Size) {size = begin;} else {int startIndex = begin + Count ; System.arraycopy (buffer, startIndex, buffer, begin, count); size-= count;}} public void Clear () {buffer = new byte[kdefaultbuffersize];size = 0;} public static void Main (string[] args) {//byteutils bu = new Byteutils ();//byte[] buf = new byte[32];//arrays.fill (buf, (byte) 1);//bu.append (buf,buf.length);//buf = new Byte[1024];//arrays.fill (buf, (byte) 2);// Bu.append (buf,buf.length);//system.out.println (Bu.getsize ());//bu.erase (0,);//system.out.println (Bu.getSize ( );//bu.erase (4);//system.out.println (Bu.getsize ());//system.out.println (Bu.getbuffer () [2]);// System.out.println (Bu.getbuffer () [1]);/}}
Note: The recently discovered Java.nio.ByteBUffer class can only set a fixed capacity and cannot be dynamically grown.
[Java]_[primary]_[practical byte processing class]