下面列出了java.util.Arrays#copyOfRange ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void prepareManyPartitionedData() throws Exception {
if (hiveManyPartitionsTable != null)
return;
hiveManyPartitionsTable = new HiveTable(HIVE_MANY_PARTITIONED_TABLE, new String[]{"t1 STRING"});
String[] partitionedColumns = Arrays.copyOfRange(HIVE_TYPES_COLS, 1, HIVE_TYPES_COLS.length);
hiveManyPartitionsTable.setPartitionedBy(partitionedColumns);
hive.createTableAndVerify(hiveManyPartitionsTable);
hive.runQuery("SET hive.exec.dynamic.partition = true");
hive.runQuery("SET hive.exec.dynamic.partition.mode = nonstrict");
hive.runQuery("SET hive.stats.autogather = false");
// Insert into table using dynamic partitioning.
// Some of the fields are NULL so they will be inserted into the default partition.
hive.insertDataToPartition(hiveTypesTable, hiveManyPartitionsTable,
new String[]{"t2", "num1", "dub1", "dec1", "tm", "r", "bg", "b", "tn", "sml", "dt", "vc1", "c1", "bin"},
new String[]{"*"});
}
public static byte[] decrypt(byte[] encryptedMessage, PrivateKey privateKey)
{
try
{
// Read the symmetric key from the encrypted message (and its length)
int length = byteArrayToInt(Arrays.copyOf(encryptedMessage,4));
byte[] wrappedKey = Arrays.copyOfRange(encryptedMessage,4,4+length);
// Decrypt the symmetric key
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key symmetricKey = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
// Decrypt the message and return it
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, symmetricKey);
return cipher.doFinal(Arrays.copyOfRange(encryptedMessage,4+length,encryptedMessage.length));
}
catch (GeneralSecurityException exception)
{
exception.printStackTrace();
return null;
}
}
private static byte[] mac_des_3des(GPKey key, byte[] text, int offset, int length, byte[] iv) {
try {
// get ciphers
Cipher cipherA = Cipher.getInstance(DES3_CBC_NOPAD);
Cipher cipherB = Cipher.getInstance(DES_CBC_NOPAD);
// get keys (truncated version for DES)
SecretKey keyA = key.getSecretKey(GPKeyCipher.DES3);
SecretKey keyB = key.getSecretKey(GPKeyCipher.DES);
// IV for final round depends on whether we have previous rounds
byte[] finalIV = iv;
// pre-final rounds
if (length > 8) {
// long messages get all but the last block hashed using DES
cipherB.init(Cipher.ENCRYPT_MODE, keyB, new IvParameterSpec(iv));
byte[] partial = cipherB.doFinal(text, offset, length - 8);
// and use the output from that as the IV for the 3DES final round
finalIV = Arrays.copyOfRange(partial, partial.length - 8, partial.length);
}
// final round
cipherA.init(Cipher.ENCRYPT_MODE, keyA, new IvParameterSpec(finalIV));
byte[] finalCipher = cipherA.doFinal(text, (offset + length) - 8, 8);
// copy result and return
return Arrays.copyOfRange(finalCipher, finalCipher.length - 8, finalCipher.length);
} catch (Exception e) {
throw new RuntimeException("MAC computation failed", e);
}
}
/**
* Creates an new {@code RX64IOPacket} object from the given payload.
*
* @param payload The API frame payload. It must start with the frame type
* corresponding to a RX64 Address IO packet ({@code 0x82}).
* The byte array must be in {@code OperatingMode.API} mode.
*
* @return Parsed RX64 Address IO packet.
*
* @throws IllegalArgumentException if {@code payload[0] != APIFrameType.RX_64.getValue()} or
* if {@code payload.length < }{@value #MIN_API_PAYLOAD_LENGTH} or
* if {@code rssi < 0} or
* if {@code rssi > 255} or
* if {@code receiveOptions < 0} or
* if {@code receiveOptions > 255}.
* @throws NullPointerException if {@code payload == null}.
*/
public static RX64IOPacket createPacket(byte[] payload) {
if (payload == null)
throw new NullPointerException("RX64 Address IO packet payload cannot be null.");
// 1 (Frame type) + 8 (64-bit address) + 1 (RSSI) + 1 (receive options)
if (payload.length < MIN_API_PAYLOAD_LENGTH)
throw new IllegalArgumentException("Incomplete RX64 Address IO packet.");
if ((payload[0] & 0xFF) != APIFrameType.RX_IO_64.getValue())
throw new IllegalArgumentException("Payload is not a RX64 Address IO packet.");
// payload[0] is the frame type.
int index = 1;
// 8 bytes of 64-bit address.
XBee64BitAddress sourceAddress64 = new XBee64BitAddress(Arrays.copyOfRange(payload, index, index + 8));
index = index + 8;
// Received Signal Strength Indicator byte.
int rssi = payload[index] & 0xFF;
index = index + 1;
// Received Options byte.
int receiveOptions = payload[index] & 0xFF;
index = index + 1;
// Get data.
byte[] data = null;
if (index < payload.length)
data = Arrays.copyOfRange(payload, index, payload.length);
return new RX64IOPacket(sourceAddress64, rssi, receiveOptions, data);
}
byte[] readSecurityBuffer(int offset) throws NTLMException {
int pos = readInt(offset+4);
if (pos == 0) return null;
try {
return Arrays.copyOfRange(
internal, pos, pos + readShort(offset));
} catch (ArrayIndexOutOfBoundsException ex) {
throw new NTLMException(NTLMException.PACKET_READ_ERROR,
"Input message incorrect size");
}
}
private final byte[] doFinal0(byte[] input, int inputOffset, int inputLen)
throws IllegalBlockSizeException, BadPaddingException {
if (doneFinal) {
if (securityViolated)
throw new SecurityException();
if (Cipher.DECRYPT_MODE == getCipherMode())
return finalBytes == null ? null : finalBytes.clone();
// final bytes must have been previously computed via encryption
int finalDataLen = finalBytes.length - tagLen;
if (inputLen == finalDataLen)
return finalBytes.clone();
if (inputLen < finalDataLen) {
if (inputLen + currentCount == outputByteCount) {
int from = finalBytes.length - tagLen - inputLen;
return Arrays.copyOfRange(finalBytes, from, finalBytes.length);
}
}
throw new IllegalStateException("Inconsistent re-rencryption");
}
doneFinal = true;
// compute final bytes for the first time
finalBytes = super.doFinal(input, inputOffset, inputLen);
if (finalBytes == null)
return null; // only possible for decryption
outputByteCount += checkMax(finalBytes.length - tagLen);
return finalBytes.clone();
}
/**
* Decompress a point on an elliptic curve.
*
* @param bytes The compressed point. Its representation is z || x where z is 2+lsb(y) and x is
* using a unsigned fixed length big-endian representation.
* @param ecParams the specification of the curve. Only Weierstrass curves over prime order fields
* are implemented.
*/
public static ECPoint decompressPoint(byte[] bytes, ECParameterSpec ecParams)
throws GeneralSecurityException {
EllipticCurve ec = ecParams.getCurve();
ECField field = ec.getField();
if (!(field instanceof ECFieldFp)) {
throw new GeneralSecurityException("Only curves over prime order fields are supported");
}
BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
int expectedLength = 1 + (p.bitLength() + 7) / 8;
if (bytes.length != expectedLength) {
throw new GeneralSecurityException("compressed point has wrong length");
}
boolean lsb;
switch (bytes[0]) {
case 2:
lsb = false;
break;
case 3:
lsb = true;
break;
default:
throw new GeneralSecurityException("Invalid format");
}
BigInteger x = new BigInteger(1, Arrays.copyOfRange(bytes, 1, bytes.length));
if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
throw new GeneralSecurityException("x is out of range");
}
// Compute rhs == x^3 + a x + b (mod p)
BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
BigInteger y = modSqrt(rhs, p);
if (lsb != y.testBit(0)) {
y = p.subtract(y).mod(p);
}
return new ECPoint(x, y);
}
/**
* Reads an array of NBT objects, which is prefixed by its own length (in the
* form of a varint)
*
* @return Parsed NBT array
* @throws EndOfPacketException Thrown if data is invalid
*/
public NBTCompound[] readNBTArray() throws EndOfPacketException {
int length = readVarInt();
NBTCompound[] result = new NBTCompound[length];
for (int i = 0; i < length; i++) {
NBTParser parser = new NBTParser(Arrays.copyOfRange(data, dataPointer, data.length));
result[i] = parser.parse();
dataPointer += parser.getLength();
}
return result;
}