📄 byte_array.java
字号:
//-*-java-*-/*Copyright (c) 2003 Asoft ltd. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice,this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer inthe documentation and/or other materials provided with the distribution.3. The names of the authors may not be used to endorse or promote productsderived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASOFTLTD. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* *Copyright (c) 2003 Asoft ltd. All rights reserved. *www.asoft.ru *Authors: Alexandre Rusev, Alexey Soloviev */package com.asoft.midp.dynarray;import java.util.Vector;public final class byte_array implements byte_AccessibleArray{ private static final boolean FAST = true; private static final int DEBUG = 1; private static final int block_size = 512; private static final int block_shr = 9; private static final int block_and = 511; private int len; private final byte[][] arrays; private final int max_blocks; private final int residue; private int total_allocated;// private String name; // public byte_array(int len){// this(len,0,null);// }// public byte_array(int len, int block_size){// this(len,block_size,null);// }// public byte_array(int len, String name){// this(len,0,name); // } public byte_array(int len) {// System.out.println("newbytearray="+len);// this.block_size=block_size; this.len = len;// this.name=name; //if(this.block_size<=0){this.block_size=100;}// this.block_size=Math.min(this.block_size,len); if (len >= 0) { int n = this.len / this.block_size; residue = this.len - n * this.block_size; if (residue > 0) { ++n; } max_blocks = n; arrays = new byte[max_blocks][]; } else { arrays = null; max_blocks = 0; residue = 0; }//if } public static void Copy(byte_AccessibleArray src, int srcoff, byte_AccessibleArray dst, int dstoff, int len) { if (len == 0) { return; } src.copyTo(srcoff, dst, dstoff, len); } public static void Copy(byte[] src, int srcoff, byte_AccessibleArray dst, int dstoff, int len) { dst.copyFrom(src, srcoff, dstoff, len); } public static final void Copy(byte_AccessibleArray src, int srcoff, byte[] dst, int dstoff, int len) { src.copyTo(srcoff, dst, dstoff, len); } public int length() { return len; } public final int totalAllocated() { return total_allocated; } public int blockSize() { return block_size; } public final void set(int n, byte val) { final int c = n >> 9; if (arrays[c] == null) { final int r = n >> block_shr; final int m; if (c == arrays.length - 1 && this.residue > 0) { m = this.residue; } else { m = this.block_size; } arrays[c] = new byte[m]; total_allocated = total_allocated + m; } final int r = n & 511; arrays[c][r] = val; } public final byte get(int n) { final int c = n >> block_shr; final int r = n & block_and; if (arrays[c] == null) { return 0; } else { return arrays[c][r]; }//if(arrays[c]==null) }//todo: optimize - very often used /**�������� �� byte[] src � ������ byte_array * */ public final void copyFrom(byte[] src, int srcoff, int dstoff, int len) {//System.out.println("copyFrom="+len); if (false) { //FAST==true Vector spl = new Vector(); final int d = dstoff - srcoff; final int end = srcoff + len; //��� ������������ � �������� � "������� ���������" //������� ��������� int pd = (dstoff / this.block_size) * this.block_size - d; pd = pd + this.block_size; spl.addElement(new Integer(srcoff)); while (pd < end) { final int last = elem(spl.size() - 1, spl); if (last < pd) { spl.addElement(new Integer(pd)); } pd = pd + this.block_size; }//wihile spl.addElement(new Integer(end)); for (int i = 0; i < spl.size() - 1; ++i) { final int b = ((Integer) spl.elementAt(i)).intValue(); final int e = ((Integer) spl.elementAt(i + 1)).intValue() - 1; final int l = e - b + 1; final int dst_b = b + d; final int dst_c = (dst_b) / this.block_size; final int dst_r = (dst_b) % this.block_size; if (this.arrays[dst_c] == null) { this.set(dst_b, (byte) -1); } System.arraycopy(src, b, this.arrays[dst_c], dst_r, l); }//for } else { //FAST==false for (int q = 0; q < len; ++q) { final int nsrc = srcoff + q; final int ndst = dstoff + q; this.set(ndst, src[nsrc]); }//for }//if(FAST) } /**�������� �� ������� byte_array � byte[] dst * *///--usually used to copy 512-4096 blocks public final void copyTo(int srcoff, byte[] dst, int dstoff, int len) {//System.out.println("copyTo="+len); //FAST==true if (FAST) { Vector spl = new Vector(); final int d = dstoff - srcoff; final int end = srcoff + len; int ps = (srcoff / this.block_size) * this.block_size; ps = ps + this.block_size; spl.addElement(new Integer(srcoff)); while (ps < end) { final int last = elem(spl.size() - 1, spl); if (last < ps) { spl.addElement(new Integer(ps)); } ps = ps + this.block_size; }//wihile spl.addElement(new Integer(end)); for (int i = 0; i < spl.size() - 1; ++i) { int b = ((Integer) spl.elementAt(i)).intValue(); int e = ((Integer) spl.elementAt(i + 1)).intValue() - 1; int l = e - b + 1; final int src_c = b / this.block_size; final int src_r = b % this.block_size; final int dst_b = b + d; if (this.arrays[src_c] != null) { System.arraycopy(this.arrays[src_c], src_r, dst, dst_b, l); } else { clear(dst, dst_b, l); }//if }//for } else { //FAST=false for (int q = 0; q < len; ++q) { final int nsrc = srcoff + q; final int ndst = dstoff + q; dst[ndst] = this.get(nsrc); }//for }//if(FAST) }//todo: optimize//--usually used to copy 2-12 bytes blocks public final void copyTo(int srcoff, byte_AccessibleArray dst, int dstoff, int len) {//System.out.println("copyTo2="+len); //FAST==true if (false) { Vector spl = new Vector(); final int d = dstoff - srcoff; final int end = srcoff + len; int ps = (srcoff / this.block_size) * this.block_size; ps = ps + this.block_size; spl.addElement(new Integer(srcoff)); while (ps < end) { final int last = elem(spl.size() - 1, spl); if (last < ps) { spl.addElement(new Integer(ps)); } ps = ps + this.block_size; }//wihile spl.addElement(new Integer(end)); for (int i = 0; i < spl.size() - 1; ++i) { int b = ((Integer) spl.elementAt(i)).intValue(); int e = ((Integer) spl.elementAt(i + 1)).intValue() - 1; int l = e - b + 1; final int src_c = b / this.block_size; final int src_r = b % this.block_size; final int dst_b = b + d; if (this.arrays[src_c] != null) {// System.arraycopy(this.arrays[src_c],src_r,dst,dst_b,l); dst.copyFrom(this.arrays[src_c], src_r, dst_b, l); } else { dst.clear(dst_b, l); }//if }//for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -