下面列出了org.apache.commons.lang3.ArrayUtils#subarray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void removeBOM() {
InputStream inputStream = getInputStream();
try {
byte[] bytes = IOUtils.toByteArray(getInputStream());
byte[] head3 = ArrayUtils.subarray(bytes, 0, BOM_UTF_8.length);
if (Arrays.equals(head3, BOM_UTF_8)) {
setContents(ArrayUtils.subarray(bytes, BOM_UTF_8.length, Integer.MAX_VALUE));
setEncoding("UTF-8"); // null if default
} else {
byte[] head2 = ArrayUtils.subarray(bytes, 0, BOM_UTF_16BE.length);
if (Arrays.equals(head2, BOM_UTF_16BE)) {
setContents(ArrayUtils.subarray(bytes, BOM_UTF_16BE.length, Integer.MAX_VALUE));
setEncoding("UTF-16BE");
} else if (Arrays.equals(head2, BOM_UTF_16LE)) {
setContents(ArrayUtils.subarray(bytes, BOM_UTF_16BE.length, Integer.MAX_VALUE));
setEncoding("UTF-16LE");
}
// Not support UTF-32 as in the Eclipse
}
} catch (IOException e) {
throw new IllegalStateException(e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}
/**
* byte[26] 转 Node
*/
public Node(byte[] bytes) {
if (bytes.length != 26)
throw new BTException("转换为Node需要bytes长度为26,当前为:" + bytes.length);
//nodeIds
nodeIdBytes = ArrayUtils.subarray(bytes, 0, 20);
//ip
ip = DHTUtil.bytes2Ip(ArrayUtils.subarray(bytes, 20, 24));
//ports
port = DHTUtil.bytes2Port( ArrayUtils.subarray(bytes, 24, 26));
nodeId= Hex.encodeHexString(nodeIdBytes);
}
private void layoutComponents(Container target, int xOffset, int yOffset, int maxwidth, int rowheight,
SizeRequirements[] xChildren, SizeRequirements[] yChildren, int start, int end, boolean ltr)
{
SizeRequirements[] children = ArrayUtils.subarray(xChildren, start, end);
int[] xOffsets = new int[children.length];
int[] xSpans = new int[children.length];
SizeRequirements.calculateTiledPositions(maxwidth, null, children, xOffsets, xSpans, ltr);
children = ArrayUtils.subarray(yChildren, start, end);
int[] yOffsets = new int[children.length];
int[] ySpans = new int[children.length];
SizeRequirements total = new SizeRequirements(rowheight, rowheight, rowheight, 0.5f);
SizeRequirements.calculateAlignedPositions(rowheight, total, children, yOffsets, ySpans, ltr);
for (int i = 0; i < children.length; i++)
{
Component c = target.getComponent(i + start);
c.setBounds((int) Math.min((long) xOffset + (long) xOffsets[i], Integer.MAX_VALUE),
(int) Math.min((long) yOffset + (long) yOffsets[i], Integer.MAX_VALUE), xSpans[i], ySpans[i]);
}
}
/**
* TODO: Test. And make sure to test with reference alleles to make sure "*" is not included.
* @param pileupElement pileup element representing the read. Never {@code null}
* @param allele query allele. Never {@code null}
* @return Whether the read contains the allele. Note that unknown can occur as well.
*/
public static Trilean doesReadContainAllele(final PileupElement pileupElement, final Allele allele) {
Utils.nonNull(pileupElement);
Utils.nonNull(allele);
final byte[] readBases = ArrayUtils.subarray(pileupElement.getRead().getBases(), pileupElement.getOffset(), pileupElement.getOffset() + allele.getBases().length);
if (readBases.length < allele.getBases().length) {
return Trilean.UNKNOWN;
}
if (allele.basesMatch(readBases)) {
return Trilean.TRUE;
} else {
return Trilean.FALSE;
}
}
public void setMaxZoomLevel(int zoomlevel)
{
if (imageData == null)
{
for (int i = 0; i <= zoomlevel; i++)
{
imageData = ArrayUtils.add(imageData, new ImageMetadata());
}
}
else if (zoomlevel < maxZoomLevel)
{
imageData = ArrayUtils.subarray(imageData, 0, zoomlevel + 1);
}
else if (zoomlevel > maxZoomLevel)
{
for (int i = maxZoomLevel + 1; i <= zoomlevel; i++)
{
imageData = ArrayUtils.add(imageData, new ImageMetadata());
}
}
maxZoomLevel = zoomlevel;
}
private RecordDesfireFile (int fileId, DesfireFileSettings fileSettings, byte[] fileData) {
super(fileId, fileSettings, fileData);
RecordDesfireFileSettings settings = (RecordDesfireFileSettings) fileSettings;
DesfireRecord[] records = new DesfireRecord[settings.curRecords];
for (int i = 0; i < settings.curRecords; i++) {
int offset = settings.recordSize * i;
records[i] = new DesfireRecord(ArrayUtils.subarray(getData(), offset, offset + settings.recordSize));
}
mRecords = records;
}
private static void checkMagicBytes(FileStatus status, byte[] data, int offset) throws IOException {
for(int i =0, v = offset; i < MAGIC_LENGTH; i++, v++){
if(ParquetFileWriter.MAGIC[i] != data[v]){
byte[] magic = ArrayUtils.subarray(data, offset, offset + MAGIC_LENGTH);
throw new IOException(status.getPath() + " is not a Parquet file. expected magic number at tail " + Arrays.toString(ParquetFileWriter.MAGIC) + " but found " + Arrays.toString(magic));
}
}
}
public static byte[] base58CheckDecode(String key, @Nullable String keyType) throws Base58CheckException {
byte[] decoded;
try {
// base58 decode
decoded = Base58.decode(key);
} catch (AddressFormatException ex) {
throw new Base58CheckException(ex.getMessage(), ex);
}
// split the byte slice
byte[] data = ArrayUtils.subarray(decoded, 0, decoded.length - 4);
byte[] checksum = ArrayUtils.subarray(decoded, decoded.length - 4, decoded.length);
if (keyType != null) {
data = ArrayUtils.addAll(data, keyType.getBytes());
}
// ripemd160 input, sign 4 bytes to compare
byte[] hash = ripemd160(data);
// if pass, return data, otherwise throw ex
// compare two checksum
boolean isEqual = true;
for (int i = 0; i < checksum.length; i++) {
if (hash[i] != checksum[i]) {
isEqual = false;
}
}
if (!isEqual) {
throw new Base58CheckException();
}
if (keyType != null) {
return ArrayUtils.subarray(data, 0, data.length - keyType.getBytes().length);
}
return data;
}
/**
* @param signatureBytes Signature in bytes
* @return {@link Signature}
*/
public static Signature of(byte[] signatureBytes) {
int recId = (int) signatureBytes[0] - 4 - 27;
BigInteger r = new BigInteger(1, ArrayUtils.subarray(signatureBytes, 1, 33));
BigInteger s = new BigInteger(1, ArrayUtils.subarray(signatureBytes, 33, signatureBytes.length));
Signature sig = new Signature(r, s);
sig.setRecId(recId);
return sig;
}
public static BigInteger obtainSerialNumber(PrivateKey key, byte[] challenge) throws TechnicalConnectorException {
byte[] encryptedNonce = ArrayUtils.subarray(challenge, 0, challenge.length - 32);
byte[] serialNrHashed = ArrayUtils.subarray(challenge, challenge.length - 32, challenge.length);
byte[] decryptedNonceBytes = ConnectorCryptoUtils.decrypt(key, RaPropertiesLoader.getProperty("etk.challenge.cipher"), encryptedNonce);
byte[] serialNr = ArrayUtils.subarray(decryptedNonceBytes, 0, 16);
byte[] calculatedHash = ConnectorCryptoUtils.calculateDigest(RaPropertiesLoader.getProperty("etk.challenge.digest"), serialNr);
if (!Arrays.equals(serialNrHashed, calculatedHash)) {
throw new IllegalArgumentException("The challenge is not valid because the hash of the decrypted serial nr found inside the challenge is not equal to the hashed serial nr attached to the challenge.");
} else {
byte[] serialNumberUnsigned = new byte[serialNr.length + 1];
System.arraycopy(serialNr, 0, serialNumberUnsigned, 1, serialNr.length);
return new BigInteger(serialNumberUnsigned);
}
}
private static byte[] read(final InputStream stream, final int maxNb) throws IOException {
final byte[] buffer = new byte[maxNb];
final int nbRead = stream.read(buffer);
if (nbRead == buffer.length) {
return buffer;
}
return ArrayUtils.subarray(buffer, 0, nbRead);
}
public void sendData(final byte data[]) throws IOException {
// log.debug("sendData on id " + local_port);
last_use = System.currentTimeMillis();
// log.debug("sendData(): " + data.length + " bytes");
if (closed) return;
try {
final ByteBuffer bb = ByteBuffer.allocate(to_socket_array.length + data.length);
bb.put(to_socket_array);
bb.put(data);
bb.flip();
final int nbytes = socket_channel.write(bb);
to_socket_array = ArrayUtils.subarray(bb.array(), nbytes, bb.array().length);
} catch (final IOException ex) {
log.warn(ex);
ex.printStackTrace();
socket_channel.close();
closed = true;
throw ex;
}
}
/**
* パケットのheadlineを返す。履歴ウィンドウで利用されます。
* 文字化けすると重たくなるのでデフォルトではASCIIで表示可能な部分のみ表示する
*/
public String getSummarizedRequest(Packet packet) {
byte[] data = packet.getDecodedData();
byte[] prefix = ArrayUtils.subarray(data, 0, Math.min(100, data.length));
prefix = StringUtils.toAscii(prefix);
return new String(prefix);
}
/**
* FIXME remove
*
* @param href
* @return
*/
private static String removePrefix(final String href) {
String[] parts = StringUtils.split(href, '/');
parts = (String[]) ArrayUtils.subarray(parts, 1, parts.length);
if (parts == null || parts.length == 0) {
return "/";
}
String joinedString = StringUtils.join(parts, '/');
if (href.endsWith("/")) {
joinedString = joinedString + "/";
}
return joinedString;
}
public static GeoWaveRowRange toRowRange(
final ByteArrayRange range,
final int partitionKeyLength) {
final byte[] startRow = range.getStart() == null ? null : range.getStart();
final byte[] stopRow = range.getEnd() == null ? null : range.getEnd();
if (partitionKeyLength <= 0) {
return new GeoWaveRowRange(null, startRow, stopRow, true, false);
} else {
byte[] partitionKey;
boolean partitionKeyDiffers = false;
if ((startRow == null) && (stopRow == null)) {
return new GeoWaveRowRange(null, null, null, true, true);
} else if (startRow != null) {
partitionKey = ArrayUtils.subarray(startRow, 0, partitionKeyLength);
if (stopRow != null) {
partitionKeyDiffers =
!Arrays.equals(partitionKey, ArrayUtils.subarray(stopRow, 0, partitionKeyLength));
}
} else {
partitionKey = ArrayUtils.subarray(stopRow, 0, partitionKeyLength);
}
return new GeoWaveRowRange(
partitionKey,
startRow == null ? null
: (partitionKeyLength == startRow.length ? null
: ArrayUtils.subarray(startRow, partitionKeyLength, startRow.length)),
partitionKeyDiffers ? null
: (stopRow == null ? null
: (partitionKeyLength == stopRow.length ? null
: ArrayUtils.subarray(stopRow, partitionKeyLength, stopRow.length))),
true,
partitionKeyDiffers);
}
}
public byte[] prepareFastSend(byte[] data) throws Exception {
int accepted_length = callOnClientPacketReceived(data);
if (accepted_length <= 0){
return null;
}
byte[] accepted = ArrayUtils.subarray(data, 0, accepted_length);
byte[] decoded = callOnClientChunkReceived(accepted);
byte[] encoded = callOnClientChunkSend(decoded);
return encoded;
}
@Override
public int[] getChunkDimensions() {
// TODO understand why there is an extra one on the end of this array
int[] chunkDimensions = layoutMessage.getChunkDimensions();
return ArrayUtils.subarray(chunkDimensions, 0, chunkDimensions.length -1);
}
public byte[] toByteArray() {
return ArrayUtils.subarray(buffer, 0, data_size);
}
public static String pathPrefix(String[] elements) {
String[] subPath = ArrayUtils.subarray(elements, 0, elements.length - 1);
return ValuePathHelper.format(subPath);
}
private byte[] getValidBytes(ByteBuffer byteBuffer) {
return ArrayUtils.subarray(byteBuffer.array(), byteBuffer.position(),
byteBuffer.limit());
}