A Bytevector class

xiaoxiao2021-03-06  41

Package com.wen;

/ ** Like the Vector Object, Except Rather Thange TRACKING DYNAMIC ARRAY OF POINTERS TRACKING DYNAMIC ARRAY OF IS SIMPLY A DYNAMIC ARRAY OF BYTES.

The Advantage Is Speed ​​and Memory Savings.

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

CopyRight (C) 1998-2000 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell IT, Share it ... add a copyright for the portions you add ...

My Goal in Giving this Away and maintaining the copyright is to hopefully direct development. "P>

The Original Source Can Be Found At

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

@author Paul Wheaton * / public class ByteVector {private byte [] buffy; // where the bytes are stored private int len ​​= 0;.. // the current number of bytes being used private int extra = 50; // how much EXTRA TO GROW WHEN GROWTH IS NEEDED

/ ** CREATE A New Bytevector with a specific initial capacity.

@Param InitialSize The Initial Capacity.

* / public bytevector (int initialsize) {buffy = new byte [initialsize];

/ ** CREATE A New Bytevector with a specific initial capacity and to becomne to grow by a specific increment.

. @Param initialSize The initial capacity

@param initialExtra How many bytes the object will grow by when more memory is needed

* / public ByteVector (int initialSize, int initialExtra) {buffy = new byte [initialSize].; Extra = INIALEXTRA;} / ** The object HAS Memory Allocated to Store A MEMORY NUMBER OF BYTES.

* / Public Bytevector () {Buffy = New Byte [extra];

/ ** The Object is inititialized to contain a copy of a byte array.

Enough memory is allocated for some growth.

@Param B A Byte Array to Copy.

* / public bytevector (byte [] b) {len = B.LENGTH; BUFFY = New Byte [LEN EXTRA]; INT i; for (i = 0; i < Len; i ) {buffy [i] = b [i];}}

/ ** The Object Is Initialized to Contain A Copy of Another Bytevector.

Enough memory is allocated for some growth.

@Param B A bytevector to copy.

* / public bytevector (bytevector b) {len = b.len; extra = b.extra; buffy = new byte [B.Buffy.Length]; int i; for (i = 0; i

/ ** get the number of bytes currently being used.

NOTTES CURRENTLY Allocated.

@Return The Number of Bytes in Use.

* / public int length () {Return Len;}

/ ** force the number of bytes to be currently used.

If you want to empty your bytevector, use setlength (0);

If the new length is longer than the existing length, the previously unused bytes will be set to zero.

@param newLength The new length.

* / public void setLength (int newLength) {if (newLength> buffy. Length) {setcapacity (newlength extra);} if (newlength> len) {// Fill New Chars with Zero While (len

/ ** SETHOW MUCH TO GROW WHEN GROWTH IS NEEDED.

Smaller Values ​​Will Usually Save Memory, But Frequent Reallocation May Take a Lot of Time.

@param howMuch The number of extra bytes to allocate when memory reallocation is required Values ​​must be greater than zero

* / public void setExtra (int howMuch) {if (howMuch> 0) {extra = howMuch;}}..

/ ** Get the number of bytes this Object Can Hold WITHOUT Doing a realocation.

@Return The Number of Bytes this Object Can Hold WITHOUT DOING A Reallocation.

* / public int getCapacity () {return buffy.length;}

/ ** Force this Object to Be Able to Hold A Specific Number of bytes without Reallocation.

Any Attempt to make the object grow big game.

Setting The Capacity To Be Smaller Than The Current Object Size Will Result In Trailing Bytes Being Clipped.

@Param HowMuch How Many Bytes Will Be The New Capacity.

* / public void setcapacity (int hoWMUCH) {byte [] b = new byte [hoWMUCH]; int Tocopy = Math.min (Len, HowMuch); Int i For (i = 0; i

@Param Index Which Byte (0 is the first byte).

@Return The Retrieved Byte. Returns a Zero if index is outside of 0..LENGTH.

* / public byte get (int index) {byte returnvalue = 0; IF (NumBers.inrange (index, 0, len - 1)) {returnvalue = buffy [index];} return returnvalue;}

/ ** Get a copy of the last byte.

@Return a copy of the last byte.

* / public byte last () {return get (len - 1);

/ ** Get a copy of a "sub-vector".

@Param index where to start copyspace.

@Param length.

@return a bytevector Object That Contains a copy of the "sub-vector".

* / public bytevector get (int Index, Int Length) {BYTEVECTOR B = New Bytevector (Length Extra); INT i; for (i = 0; I

/ ** COPY The EXISTING OBJECT INTO A BYTE ARRAY.

@Return a copy of this object as a byte array.

* / public byte [] TobyTearray () {byte [] b = new byte [len]; int i; for (i = 0; i

@param index Which byte to set. If index references a byte beyond the last byte of the object, the object will be lengthened and the last byte will be set appropriately (undefined bytes will be set to zero).

@param b The Byte to Be Placed At Index.

* / public void set (int index, byte b) {if (index> 0) {i (index> = len) {setlength (index 1);} buffy [index ] = b;}}

/ ** add one byte to the end of the vector.

@Param B The byte to append.

* / public void append (byte b) {if (len == buffy.Length) {setcapacity (len extra);} buffy [len] = b; len ;}

/ ** Add an array of bytes to the end of this object.

@Param b the array containing the bytes to be appended.

@Param Numbytes the number of bytes to append from b * / public void append (byte [] b, int numbertes) {if (numbytes> B.LENGTH) { NumBytes = B.LENGTH;} int newlen = len numbertes; if (newlen> buffy.Length) {setcapacity (newlen extra);} int i; for (i = 0; i

@Param B The byte array to be appended.

* / public void appended (byte [] b) {int newlen = len b.length; if (newlen> buffy.Length) {setcapacity (newlen extra); } INT i; for (i = 0; i

/ ** Add all of the bytes in another bytevector to the end of this object.

@Param B The bytevector to be appended.

* / public void append (bytevector b) {int newlen = len b.len; if (newlen> buffy.length) {setcapacity (newlen extra);} int i ; For (i = 0; i

/ ** Insert a byte immediately before a particular byte in the object.

@Param B The byte to be inserted.

@Param Index WHERE to INSERT B. The byte currently at index will me moved to the right.

* / public void insert (byte b, int index) {IF Len == buffy.Length) {setcapacity (len extra);} // move bytes from index on one to the right int i; for (i = le - 1; i> = index; i--) {buffy [ i 1] = BUFFY [I];

Buffy [index] = b; len ;}

Private void insertjunk (int.com) {int newlen = len howmany; if (newlen> buffy.Length) {setcapacity (newlen extra);

//move bytes from index on to the right int I; for (i = len - 1; i> = index; i--) {buffy [i howmany] = buffy [i];}

Len = newlen;

/ ** Insert An Array of Bytes Immediately BEFORE A Particular Byte in The Object.

@Param B The array of bytes to be inserted.

@Param index where to insert b. The byte currently at index will me moved to the right.

* / public void insert (byte [] B, int index ) {INSERTJUNK (B.LENGTH, INDEX);

// Copy B Int i; for (i = 0; i

/ ** Remove bytes from your object.

@Param Index Referencing The First Byte to Be Removed.

@Param HowMany The Number of Bytes To Remove.

* / Public Void Delete (int 4 = LEN - HOWMANY; INT i; For (i = index; i

@Param B The Byte That Will Be appended to the end of your object.

* / public void push (byte b) {append (b);}

/ ** if you wish to Treat Your Object Like A Stack, You Can Push and Pop Bytes.

Remember the old assembly programming pearl: May all your pushes be popped.

@Return the last byte in your object. if the all def., a zero is returned.

* / public byte pop () {byte returnval = 0; if (len> 0) {LEN - Returnval = buffy [len];} return returnval;}

/ ** a fast way to test the first few bytes of your object.

@Param b An array of bytes to compare to.

@Return True if all of the bytes in b exactly matches the first few Bytes in your object.

* / public boolean startswith (byte [] b) {boolean ReturnValue = false; if (len> b.length) {boolean match = true; int i; for (i = 0; i

@Param b the byte to search for.

@Param Startpos the byte position in Your Object to start looking for b.

@return the position of the byte trims b Where 0 is The first byte of the object. -1 is returned if a match.

* / public int indexof (byte b, int startpos) {boolean done = false; while (! Done) {if (startpos> = len) {startpos = -1; DONE = true;} else {i (buffy [startpos] == b) {done = true;} else {startpos ;}}} returnose;

/ ** Find the position for bytes.

@param b A byte array representing the byte sequence to search for.

@param startPos The byte position in your object to start looking for b.

@return -1 is returned if a match could not be found. Otherwise The position of the first byte of the match is returned.

* / public int indexof (byte [] b, int startpos) {if (b.length> 0) {boolean done = false; while (! done) { StartPos = indexof (B [0], startpos); if (startpos == -1) {DONE = true;} else {ix (startpos b.length> LEN) {startpos = -1; done = true;} else {Boolean match = true; int i; for (i = 1; i

/ ** Find the position for byte in your object.

in Your Object.

@Param b The byte to search for.

@Return the position of the byte trims b. -1 is returned if a matching byte could not be found.

* / public int indexof (BYTE B) {Return IndexOf (b, 0);}

/ ** Find the position for bytes.

Searching Begins with the first byte in your object.

@Param B A Byte Sequence to Search for.

@return -1 is returned if a match could not be found. Otherwise the position of the first byte of the match is returned.

* / public INDEXOF (byte [] b) {Return IndexOf (b, 0);}

/ ** Replace All instances of one byte with anonyr byte.

All Occurances Are Replaced.

Searching for the "Next Instance" of a byte will beginning, immediately after the last replacement so there is no one of an infinite loop.

@Param B1 the byte to search for.

@Param b2 The byte tria is to replace b1.

* / public void replace (byte B1, byte b2) {INT i; for (i = 0; i < Len; i ) {IF (buffy [i] == b1) {buffy [i] = b2;}}}

Private void copybytesto (byte [] b, int index) {INT i; for (i = 0; i

/ ** Replace all instances of one byte with several bytes.

All Occurances Are Replaced.

search for the "Next Instance" of a byte will beginning, immediately after the last replacement so what there is no chance of an infinite loop.

@Param B1 The byte to search for.

@Param B2 The array of bytes That is to replace b1.

* / public void replace (byte b1, byte [] b2) {int pos = 0; while POS! = -1) {POS = IndexOf (B1, POS); if (POS! = -1) {IF (B2.Length> 1) {INSERTJUNK (B2.LENGTH - 1, POS);} else IF (b2 .length == 0) {Delete (POS, 1);} CopyByTesto (B2, POS); POS = b2.length;}}}

/ ** Search for a particular sequence of bytes and replace the with one byte.

All Occurances Are Replaced.

Searching for the "Next Instance" of a byte sequence will beg, immediately after the last replacement so what there is no chance of an infinite loop.

@Param B1 a byte sequence to search for.

@Param b2 The byte tria is to replace b1.

* / public void replace (byte [] b1, byte b2) {int POS = 0 WHILE (POS! = -1) {POS = INDEXOF (B1, POS); IF (POS! = -1) {if (b1.length> 1) {Delete (POS, B1.LENGTH - 1);} buffy [POS] = B2; POS ;}}} / ** Search for a Particular Sequence of Bytes and replace it with a different sequence of bytes.

All Occurances Are Replaced.

Searching for the "Next Instance" of a byte sequence will beg, immediately after the last replacement so what there is no chance of an infinite loop.

@Param B1 A Byte Sequence To Search for.

@Param B2 A Byte Array Representing A Byte Sequence That Is To Replace B1.

* / public void replace (byte [] b1, byte [] B2) {INT POS = 0; While (POS! = -1) {POS = IndexOf (B1, POS); if (POS! = -1) {IF (b1.length b2.length) {delete (pOS, b1.length - b2.length);} CopybookTesto (B2, POS); POS = b2.length; }}}

}


New Post(0)