List of usage examples for java.util Arrays copyOfRange
public static boolean[] copyOfRange(boolean[] original, int from, int to)
From source file:net.ripe.rpki.commons.crypto.util.Asn1Util.java
public static DERBitString resourceToBitString(UniqueIpResource resource, int bitCount) { int resourceTypeByteSize = resource.getType().getBitSize() / Byte.SIZE; byte[] value = resource.getValue().toByteArray(); byte[] padded; if (value.length > resourceTypeByteSize) { // Skip extra sign byte added by BigInteger. padded = Arrays.copyOfRange(value, 1, value.length); } else if (value.length < resourceTypeByteSize) { // Pad with leading zero bytes (e.g. 0.12.0.0) padded = new byte[resourceTypeByteSize]; System.arraycopy(value, 0, padded, resourceTypeByteSize - value.length, value.length); } else {//from www. j a va 2s. c o m padded = value; } assert padded.length == resourceTypeByteSize : "incorrect padded length"; int byteCount = (bitCount + Byte.SIZE - 1) / Byte.SIZE; int unusedBits = Byte.SIZE - 1 - ((bitCount + Byte.SIZE - 1) % Byte.SIZE); return new DERBitString(ArrayUtils.subarray(padded, 0, byteCount), unusedBits); }
From source file:com.opengamma.analytics.financial.timeseries.returns.ExcessSimpleNetTimeSeriesReturnCalculator.java
/** * @param x//www. java 2 s .c o m * An array of DoubleTimeSeries. The series <b>must</b> contain at * least four elements; the asset price series, the dividend price * series (can be null but it must be the second element), the * reference price series and the reference dividend series. Any * further elements will be ignored. * @throws IllegalArgumentException * If the array is null * @throws TimeSeriesException * Throws an exception if: the array is null; the array has less * than two elements; the calculation mode is strict and the price * series are not the same length. * @return A DoubleTimeSeries containing the excess return series. */ @Override public LocalDateDoubleTimeSeries evaluate(final LocalDateDoubleTimeSeries... x) { Validate.notNull(x, "x"); if (x.length < 4) { throw new TimeSeriesException("Time series array must contain at least four elements"); } if (getMode() == CalculationMode.STRICT && x[0].size() != x[2].size()) { throw new TimeSeriesException("Asset price series and reference price series were not the same size"); } final LocalDateDoubleTimeSeries assetReturn = x[1] == null ? _returnCalculator.evaluate(x[0]) : _returnCalculator.evaluate(Arrays.copyOfRange(x, 0, 2)); final LocalDateDoubleTimeSeries referenceReturn = x[3] == null ? _returnCalculator.evaluate(x[2]) : _returnCalculator.evaluate(Arrays.copyOfRange(x, 2, 4)); return assetReturn.subtract(referenceReturn); }
From source file:com.dsh105.nexus.command.module.action.AbstractActionCommand.java
public String getActionText(String... args) { Validate.notNull(args);/*w w w .j a v a2 s . com*/ if (args.length < 1) { throw new InvalidInputException("Must specify a target user to perform the action on."); } String sentenceList = StringUtil.buildSentenceList(args[0].split(",")); String additional = " "; if (args.length > 1) { additional += StringUtil.join(Arrays.copyOfRange(args, 1, args.length), " "); } return verb + (isOverriden() ? " " : "s ") + sentenceList + additional; }
From source file:com.cdancy.artifactory.rest.util.ArtifactoryUtils.java
public static GAVCoordinates gavFromURL(URL url, URL endpoint) { GAVCoordinates gavCoordinates = new GAVCoordinates(); String sanitizedURL = getBasePath(url, endpoint.toString()); String[] sanitizedURLParts = sanitizedURL.split("/"); List<String> gavArray = Arrays.asList(Arrays.copyOfRange(sanitizedURLParts, 2, sanitizedURLParts.length)); List<String> groupArray = gavArray.subList(0, gavArray.size() - 3); gavCoordinates.group = collectionToString(groupArray, "."); gavCoordinates.artifact = gavArray.get(groupArray.size()); gavCoordinates.version = gavArray.get(groupArray.size() + 1); return gavCoordinates; }
From source file:bachelorthesis.captchabuilder.builder.TextParser.java
/** * Parses the string arguments for rendering Text, creates a * TextProducer and WordRenderer passes these to the CaptchaBuilder * * @param buildSequenceOptions the string arguments for adding text * @param builder the CaptchaBuilder Object to be modified * <p/>/*from www. ja va 2 s . c o m*/ * @return a modified CaptchaBuilder object * <p/> * @throws org.apache.commons.cli.ParseException * @see CaptchaBuilder */ public static CaptchaBuilder parse(String[] buildSequenceOptions, CaptchaBuilder builder) throws ParseException { for (String textOptionArg : buildSequenceOptions) { if (!textOptionArg.isEmpty()) { try { String[] optionArgs = textOptionArg.split(CaptchaConstants.buildSequencelvl3Delim); TextOptions textOptionType = TextOptions.valueOf(optionArgs[0]); String[] textOptions = Arrays.copyOfRange(optionArgs, 1, optionArgs.length); parseTextOption(textOptionType, textOptions, builder); } catch (IllegalArgumentException e) { throw new ParseException(e.getMessage()); } } } //return builder.addText(textProducerBuilder.create(), wordRendererBuilder.create()); builder.addBuildSequence(textProducerBuilder); builder.addBuildSequence(wordRendererBuilder); return builder; }
From source file:duthientan.mmanm.com.CipherRSA.java
@Override public void encrypt(String filePath) { try {/* ww w . j a v a2s . co m*/ Cipher ecipher = Cipher.getInstance("RSA"); ecipher.init(Cipher.ENCRYPT_MODE, publicKey); Path path = Paths.get(filePath); String encryptFilePath = path.getParent().toString() + "/" + "RSA" + "_" + path.getFileName().toString(); byte[] data = Files.readAllBytes(path); byte[] textEncrypted = null; int chunkSize = 245; if (data.length < 245) { textEncrypted = ecipher.doFinal(data); } else { for (int i = 0; i < data.length; i += chunkSize) { byte[] segment = Arrays.copyOfRange(data, i, i + chunkSize > data.length ? data.length : i + chunkSize); byte[] segmentEncrypted = ecipher.doFinal(segment); textEncrypted = ArrayUtils.addAll(textEncrypted, segmentEncrypted); } } FileOutputStream fos = new FileOutputStream(encryptFilePath); fos.write(textEncrypted); fos.close(); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidKeyException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalBlockSizeException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } catch (BadPaddingException ex) { Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ste.web.http.api.RRequest.java
/** * /*from w w w. ja v a2 s . c o m*/ * @param request - NOT NULL * @param body - MAY BE NULL * * @throws URISyntaxException if the given request has an invalid URI * @throws IllegalArgumentException if request is null * */ public RRequest(final RequestLine request, final JSONObject body) throws URISyntaxException { if (request == null) { throw new IllegalArgumentException("request can not be null"); } uri = new URI(request.getUri()); String[] elements = StringUtils.split(uri.getPath(), '/'); if (elements.length < 3) { throw new URISyntaxException(uri.toString(), "invalid rest request; a valid rest url shall follow the syntax /api/<action>/<resource>"); } action = elements[1]; handler = elements[2]; resource = Arrays.copyOfRange(elements, 2, elements.length); this.body = body; }
From source file:com.ethercamp.harmony.keystore.KeystoreFormat.java
public String toKeystore(final ECKey key, String password) { try {//from w ww .j a v a 2s. c o m // n,r,p = 2^18, 8, 1 uses 256MB memory and approx 1s CPU time on a modern CPU. // final int ScryptN = ((Double) Math.pow(10.0, 18.0)).intValue(); final int ScryptN = 262144; final int ScryptR = 8; final int ScryptP = 1; final int ScryptDklen = 32; // salt final byte[] salt = generateRandomBytes(32); final byte[] derivedKey = scrypt(password.getBytes(), salt, ScryptN, ScryptR, ScryptP, ScryptDklen); // 128-bit initialisation vector for the cipher (16 bytes) final byte[] iv = generateRandomBytes(16); final byte[] privateKey = key.getPrivKeyBytes(); final byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16); final byte[] cipherText = encryptAes(iv, encryptKey, privateKey); final byte[] mac = HashUtil.sha3(concat(Arrays.copyOfRange(derivedKey, 16, 32), cipherText)); final KeystoreItem keystore = new KeystoreItem(); keystore.address = Hex.toHexString(key.getAddress()); keystore.id = UUID.randomUUID().toString(); keystore.version = 3; keystore.crypto = new KeystoreCrypto(); keystore.crypto.setKdf("scrypt"); keystore.crypto.setMac(Hex.toHexString(mac)); keystore.crypto.setCipher("aes-128-ctr"); keystore.crypto.setCiphertext(Hex.toHexString(cipherText)); keystore.crypto.setCipherparams(new CipherParams()); keystore.crypto.getCipherparams().setIv(Hex.toHexString(iv)); keystore.crypto.setKdfparams(new KdfParams()); keystore.crypto.getKdfparams().setN(ScryptN); keystore.crypto.getKdfparams().setR(ScryptR); keystore.crypto.getKdfparams().setP(ScryptP); keystore.crypto.getKdfparams().setDklen(ScryptDklen); keystore.crypto.getKdfparams().setSalt(Hex.toHexString(salt)); ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(keystore); } catch (Exception e) { log.error("Problem storing key", e); throw new RuntimeException("Problem storing key. Message: " + e.getMessage(), e); } }
From source file:com.ibm.crail.storage.nvmf.NvmfStorageTier.java
public void init(CrailConfiguration crailConfiguration, String[] args) throws IOException { if (initialized) { throw new IOException("NvmfStorageTier already initialized"); }//from w w w .j a v a2s. com initialized = true; NvmfStorageConstants.updateConstants(crailConfiguration); if (args != null) { Options options = new Options(); Option bindIp = Option.builder("a").desc("ip address to bind to").hasArg().build(); Option port = Option.builder("p").desc("port to bind to").hasArg().type(Number.class).build(); Option pcieAddress = Option.builder("s").desc("PCIe address of NVMe device").hasArg().build(); options.addOption(bindIp); options.addOption(port); options.addOption(pcieAddress); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine line = null; try { line = parser.parse(options, Arrays.copyOfRange(args, 0, args.length)); if (line.hasOption(port.getOpt())) { NvmfStorageConstants.PORT = ((Number) line.getParsedOptionValue(port.getOpt())).intValue(); } } catch (ParseException e) { System.err.println(e.getMessage()); formatter.printHelp("NVMe storage tier", options); System.exit(-1); } if (line.hasOption(bindIp.getOpt())) { NvmfStorageConstants.IP_ADDR = InetAddress.getByName(line.getOptionValue(bindIp.getOpt())); } if (line.hasOption(pcieAddress.getOpt())) { NvmfStorageConstants.PCIE_ADDR = line.getOptionValue(pcieAddress.getOpt()); } } NvmfStorageConstants.verify(); }
From source file:ch.jamiete.hilda.commands.ChannelSeniorCommand.java
/** * Determines which subcommand to run, or sends a help message *//*from ww w. java2 s. c o m*/ @Override public void execute(final Message message, final String[] args, final String label) { final Member member = message.getGuild().getMember(message.getAuthor()); if (args.length == 0) { this.help(message.getTextChannel(), member); return; } final ChannelCommand command = this.subcommands.stream() .filter(c -> c.getName().equalsIgnoreCase(args[0]) || c.hasAlias(args[0])).findFirst().orElse(null); if (command == null) { this.help(message.getTextChannel(), member); return; } if (command.getMinimumPermission() != null && !member.hasPermission(message.getTextChannel(), command.getMinimumPermission())) { this.reply(message, "You don't have permission to use that command"); return; } Hilda.getLogger().fine("Executing subcommand " + command.getName()); command.execute(message, Arrays.copyOfRange(args, 1, args.length), args[0]); }