Java Code Examples for org.apache.commons.lang.ArrayUtils.subarray()

The following are Jave code examples for showing how to use subarray() of the org.apache.commons.lang.ArrayUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: logistimo-web-service   File: HDFSBlobStoreService.java   View Source Code Vote up 6 votes
public byte[] fetchData(String blobKey, long startIndex, long l) {
  CountingInputStream inputStream = new CountingInputStream(getInputStream(blobKey));
  byte[] bytes = new byte[(int) l];
  try {
    int readSize = inputStream.read(bytes, (int) startIndex, (int) l);
    if (readSize < l) {
      bytes = ArrayUtils.subarray(bytes, 0, readSize - 1);
    }
  } catch (IOException e) {
    LOGGER.warn("Failed to read bytes", e);
  } finally {
    try {
      inputStream.close();
    } catch (IOException ignored) {
      LOGGER.warn("Exception while closing inputStream", ignored);
    }
  }
  return bytes;
}
 
Example 2
Project: MystiCraft   File: CommandManager.java   View Source Code Vote up 5 votes
boolean execute(CommandSender sender, String label, String[] strs) {
	strs = (String[]) ArrayUtils.subarray(strs, cmd.label().split("\\.").length-1, strs.length);
	CommandContext ctx = new CommandContext(sender, label, strs);
	if (this.args != null) {
		for (int i = 0; i < this.args.length; i++) {
			if (strs.length <= i) {
				break;
			}
			if (!ctx.bindParamater(this.args[i], strs[i])) {
				sender.sendMessage(cmd.usage());
				return false;
			}
		}
		if (strs.length < this.args.length) {
			for (int i = strs.length; i < this.args.length; i++) {
				if (this.args[strs.length].required()) {
					sender.sendMessage(cmd.usage());
					return false;
				}
			}
		}
	}
	
	try {
		return (boolean) this.m.invoke(this.o, ctx);
	} catch (Exception e) {
		e.printStackTrace();
		sender.sendMessage(cmd.usage());
		return false;
	}
}
 
Example 3
Project: TestDemo1-github   File: PaymentUtils.java   View Source Code Vote up 5 votes
/**
 * 内容加密
 * 
 * @param encryptStr
 * @param publicKey
 * @return
 * @throws Exception
 */
public static String encrypt(String content, String publicKey) throws Exception {
	byte[] publicKeyBytes = Base64.decodeBase64(publicKey);
	byte[] encryptBytes = content.getBytes("UTF-8");

	if (encryptBytes.length <= ENCRYPT_BlOCK_SIZE) {
		return Base64.encodeBase64String(encrypt(encryptBytes, publicKeyBytes));
	} else {
		byte[] buffer = null;
		byte[] blockBytes = new byte[ENCRYPT_BlOCK_SIZE];

		int index = ((encryptBytes.length - 1) / ENCRYPT_BlOCK_SIZE) + 1;

		for (int i = 0; i < index; i++) {
			if (i == (index - 1)) {
				blockBytes = new byte[ENCRYPT_BlOCK_SIZE];
			}
			int startIndex = i * ENCRYPT_BlOCK_SIZE;
			int endIndex = startIndex + ENCRYPT_BlOCK_SIZE;
			blockBytes = ArrayUtils.subarray(encryptBytes, startIndex, endIndex);
			if (buffer == null) {
				buffer = encrypt(blockBytes, publicKeyBytes);
			} else {
				buffer = ArrayUtils.addAll(buffer, encrypt(blockBytes, publicKeyBytes));
			}

		}
		return Base64.encodeBase64String(buffer);
	}
}
 
Example 4
Project: TestDemo1-github   File: PaymentUtils.java   View Source Code Vote up 5 votes
/**
 * 内容解密
 * 
 * @param decryptStr
 * @param privateKey
 * @return
 * @throws Exception
 */
public static String decrypt(String content, String privateKey) throws Exception {
	byte[] privateKeyBytes = Base64.decodeBase64(privateKey);

	byte[] decryptBytes = Base64.decodeBase64(content);

	if (decryptBytes.length <= DECRYPT_BLOCK_SIZE) {
		return new String(decrypt(decryptBytes, privateKeyBytes), "UTF-8");
	} else {
		byte[] buffer = null;

		int index = ((decryptBytes.length - 1) / DECRYPT_BLOCK_SIZE) + 1;
		byte[] blockBytes = new byte[DECRYPT_BLOCK_SIZE];
		for (int i = 0; i < index; i++) {
			if (i == index - 1) {
				blockBytes = new byte[DECRYPT_BLOCK_SIZE];
			}
			int startIndex = i * DECRYPT_BLOCK_SIZE;
			int endIndex = startIndex + DECRYPT_BLOCK_SIZE;
			blockBytes = ArrayUtils.subarray(decryptBytes, startIndex,
					endIndex > decryptBytes.length ? decryptBytes.length : endIndex);
			if (buffer == null) {
				buffer = decrypt(blockBytes, privateKeyBytes);
			} else {
				buffer = ArrayUtils.addAll(buffer, decrypt(blockBytes, privateKeyBytes));
			}
		}
		return new String(buffer, "UTF-8");
	}
}
 
Example 5
Project: poppynotes   File: NoteEncryptionServiceImpl.java   View Source Code Vote up 5 votes
private Key createKey(String key, String principalName) {
	byte[] keyBytes = decodeBase64(key);
	byte[] principalBytes = getPrincipalWithoutEmailTail(principalName).getBytes(UTF_8);
	
	if(principalBytes.length > MAX_KEY_SIZE_BYTES){
		principalBytes = ArrayUtils.subarray(principalBytes, 0, MAX_KEY_SIZE_BYTES);
	}
	keyBytes = concatenateByteArrays(keyBytes,principalBytes);
	return new SecretKeySpec(keyBytes, 0, keyBytes.length, ENCRYPTION_ALGORITHM);
}
 
Example 6
Project: SparkSeq   File: HaplotypeCaller.java   View Source Code Vote up 5 votes
private byte[] getTrimedRefBases(ActiveRegion originalRegion, ActiveRegion trimedRegion, byte[] originalRefBases) {
    GenomeLoc originalLoc = originalRegion.getExtendedLoc();
    GenomeLoc trimedLoc = trimedRegion.getExtendedLoc();
    if(originalLoc.getStart() > trimedLoc.getStart() || originalLoc.getStop() < trimedLoc.getStop()) {
        throw new PipelineException("Error !! implement error when get trimed ref base");
    }
    int startIdx = trimedLoc.getStart() - originalLoc.getStart();
    int endIdx = originalRefBases.length + trimedLoc.getStop() - originalLoc.getStop();
    return ArrayUtils.subarray(originalRefBases, startIdx, endIdx);
}
 
Example 7
Project: poppynotes   File: EncryptionUtils.java   View Source Code Vote up 4 votes
public static byte[] concatenateByteArrays(byte[] first, byte[] second){
	byte[] keyBytes = ArrayUtils.subarray(first, 0, first.length - second.length);
	return ArrayUtils.addAll(keyBytes, second);
}
 
Example 8
Project: obevo   File: Main.java   View Source Code Vote up 4 votes
/**
 * Executes the main method. This is public so that client distributions can test this method from their own
 * packages.
 */
@VisibleForTesting
public void execute(String[] args, Runnable exitSuccessMethod, Runnable exitFailureMethod) {
    Pair<String, Procedure<String[]>> commandEntry = getDeployCommand(args, exitFailureMethod);

    LogUtil.FileLogger logAppender = LogUtil.getLogAppender(commandEntry.getOne());

    StopWatch changeStopWatch = new StopWatch();
    changeStopWatch.start();
    LOG.info("Starting action at time [" + new Date() + "]");
    boolean success = false;

    Throwable processException = null;
    try {
        String[] argSubset = (String[]) ArrayUtils.subarray(args, 1, args.length);

        commandEntry.getTwo().value(argSubset);
        success = true;
    } catch (Throwable t) {
        processException = t;
    } finally {
        // We handle the exception and do system.exit in the finally block as we want a clean message to go out to users.
        // If we just threw the runtime exception, then that would be the last thing that appears to users, which
        // was confusing for users.

        changeStopWatch.stop();
        long runtimeSeconds = changeStopWatch.getTime() / 1000;
        String successString = success ? "successfully" : "with errors";
        LOG.info("");
        LOG.info("Action completed {} at {}, took {} seconds.", successString, new Date(), runtimeSeconds);
        LOG.info("");
        if (processException != null) {
            LOG.info("*** Exception stack trace ***", processException);
            LOG.info("");
        }
        LOG.info("Detailed Log File is available at: {}", logAppender.getLogFile());
        LOG.info("");
        LOG.info("Exiting {}!", successString);
        IOUtils.closeQuietly(logAppender);
        if (processException != null) {
            exitFailureMethod.run();
        } else {
            exitSuccessMethod.run();
        }
    }
}