List of usage examples for java.util Arrays copyOfRange
public static boolean[] copyOfRange(boolean[] original, int from, int to)
From source file:com.aliyun.openservices.odps.console.commands.logview.GetLogAction.java
@Override public void parse(String[] args) throws LogViewArgumentException { if (args.length < 1) { throw new LogViewArgumentException("A log entry must be specified"); }//w ww.j a v a2 s. c om fid = args[0]; Options options = getOptions(); CommandLineParser parser = new GnuParser(); try { if (args.length > 1) { CommandLine cl = parser.parse(options, Arrays.copyOfRange(args, 1, args.length)); configure(cl); } } catch (ParseException e) { throw new LogViewArgumentException(e.getMessage()); } }
From source file:com.rom.jmultipatcher.Utils.java
public static long getCRC32(final String filepath, final int bytesBeforeEnd) throws IOException { final CRC32 sum_control = new CRC32(); final byte[] fileAsByteArray = FileUtils.readFileToByteArray(new File(filepath)); final byte[] copyOfRange = Arrays.copyOfRange(fileAsByteArray, 0, fileAsByteArray.length - bytesBeforeEnd); sum_control.update(copyOfRange);/*www. j a v a2 s. c o m*/ return sum_control.getValue(); }
From source file:cn.patterncat.metrics.utils.MetricNamingUtil.java
/** * Joins an array of strings into a composite name. This method sanitizes each element of the array by trimming it * and replacing illegal characters with {@link MetricNamingUtil#REPLACEMENT_CHAR}. An illegal character is any * character but "\w", "-", and {@link MetricNamingUtil#SEPARATOR}. * * @param nameParts array of strings to combine into a composite name. * @return a composite name, comprised of sanitized elements of the passed in array, delimited by the separator. * @throws NullPointerException if the passed in array is null. * @throws IllegalArgumentException if the passed in array is empty. *//*from w w w . j a v a 2s .com*/ public static String join(String[] nameParts) { Preconditions.checkNotNull(nameParts, "nameParts cannot be null"); Preconditions.checkArgument(nameParts.length > 0, "At least one namePart must be provided!"); return join(nameParts[0], Arrays.copyOfRange(nameParts, 1, nameParts.length)); }
From source file:de.csdev.ebus.command.datatypes.ext.EBusTypeDateTime.java
@Override public EBusDateTime decodeInt(byte[] data) throws EBusTypeException { IEBusType<Object> dateType = getDateType(); IEBusType<Object> timeType = getTimeType(); byte[] timeData = null; byte[] dateData = null; if (timeFirst) { timeData = Arrays.copyOf(data, timeType.getTypeLength()); dateData = Arrays.copyOfRange(data, timeData.length, timeData.length + dateType.getTypeLength()); } else {/* w w w . ja v a 2s . c o m*/ dateData = Arrays.copyOf(data, dateType.getTypeLength()); timeData = Arrays.copyOfRange(data, dateData.length, dateData.length + timeType.getTypeLength()); } EBusDateTime time = (EBusDateTime) timeType.decode(timeData); EBusDateTime date = (EBusDateTime) dateType.decode(dateData); Calendar calendar = date.getCalendar(); calendar.set(Calendar.HOUR_OF_DAY, time.getCalendar().get(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, time.getCalendar().get(Calendar.MINUTE)); calendar.set(Calendar.SECOND, time.getCalendar().get(Calendar.SECOND)); return new EBusDateTime(calendar, false, false); }
From source file:bachelorthesis.captchabuilder.builder.BorderParser.java
/** * Parses the string arguments for rendering a border, creates a * BorderProducer and passes it to the CaptchaBuilder * <p/>//from w ww . ja v a2s. com * @param borderOptions the string arguments for building a border * @param builder the CaptchaBuilder Object to be modified * <p/> * @return a modified CaotchaBuilder object * <p/> * @throws ParseException * @see CaptchaBuilder */ public static CaptchaBuilder parse(String[] borderOptions, CaptchaBuilder builder) throws ParseException { if (borderOptions.length == 0) { //return builder.addBorder(); builder.addBuildSequence(new BorderProducerBuilder(BorderProducerType.SOLID)); return builder; } if (borderOptions.length > 1) { throw new ParseException("Border takes a max of 1 arguments"); } for (String borderOption : borderOptions) { if (!borderOption.isEmpty()) { try { String[] optionArgs = borderOption.split(CaptchaConstants.buildSequencelvl3Delim); BorderProducerType borderProducerType = BorderProducerType.valueOf(optionArgs[0]); String[] borderOptionArgs = Arrays.copyOfRange(optionArgs, 1, optionArgs.length); return parseBorderProducer(borderProducerType, borderOptionArgs, builder); } catch (IllegalArgumentException e) { throw new ParseException(e.getMessage()); } } } return builder; }
From source file:com.titankingdoms.dev.titanchat.command.defaults.PrivMsgCommand.java
@Override public void execute(CommandSender sender, String[] args) { Participant target = plugin.getParticipantManager().getParticipant(args[0]); if (!target.isOnline()) { sendMessage(sender, target.getDisplayName() + " &4is currently offline"); return;//from w w w . j a v a 2 s . c o m } Participant participant = plugin.getParticipantManager().getParticipant(sender); participant.link(target); sendMessage(sender, "&6You have started a private conversation with " + target.getDisplayName()); target.notice(participant.getDisplayName() + " &6has started a private conversation with you"); String message = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " "); if (args.length > 1) target.processConversation(participant, target.getConversationFormat(), message); }
From source file:com.opengamma.analytics.math.curve.InterpolatedCurveBuildingFunction.java
public LinkedHashMap<String, InterpolatedDoublesCurve> evaluate(DoubleMatrix1D x) { Validate.notNull(x, "null data x"); Validate.isTrue(_nNodes == x.getNumberOfElements(), "x wrong length"); LinkedHashMap<String, InterpolatedDoublesCurve> res = new LinkedHashMap<String, InterpolatedDoublesCurve>(); int index = 0; for (final String name : _interpolators.keySet()) { final Interpolator1D interpolator = _interpolators.get(name); final double[] nodes = _knotPoints.get(name); final double[] values = Arrays.copyOfRange(x.getData(), index, index + nodes.length); index += nodes.length;//from w ww .j a v a2s . c o m InterpolatedDoublesCurve curve = InterpolatedDoublesCurve.from(nodes, values, interpolator); res.put(name, curve); } return res; }
From source file:com.marklogic.contentpump.ContentPump.java
public static int runCommand(String[] args) throws IOException { // get command String cmd = args[0];// ww w .j av a2 s .co m if (cmd.equalsIgnoreCase("help")) { printUsage(); return 1; } else if (cmd.equalsIgnoreCase("version")) { logVersions(); return 1; } Command command = Command.forName(cmd); // get options arguments String[] optionArgs = Arrays.copyOfRange(args, 1, args.length); if (LOG.isDebugEnabled()) { LOG.debug("Command: " + command); StringBuilder buf = new StringBuilder(); for (String arg : optionArgs) { buf.append(arg); buf.append(' '); } LOG.debug("Arguments: " + buf); } // parse hadoop specific options Configuration conf = new Configuration(); GenericOptionsParser genericParser = new GenericOptionsParser(conf, optionArgs); String[] remainingArgs = genericParser.getRemainingArgs(); // parse command specific options CommandlineOptions options = new CommandlineOptions(); command.configOptions(options); CommandLineParser parser = new GnuParser(); CommandLine cmdline; try { cmdline = parser.parse(options, remainingArgs); } catch (Exception e) { LOG.error("Error parsing command arguments: "); LOG.error(e.getMessage()); // Print the command usage message and exit. command.printUsage(command, options.getPublicOptions()); return 1; // Exit on exception here. } for (String arg : cmdline.getArgs()) { LOG.error("Unrecognized argument: " + arg); // Print the command usage message and exit. command.printUsage(command, options.getPublicOptions()); return 1; // Exit on exception here. } // check running mode and hadoop conf dir configuration String mode = cmdline.getOptionValue(MODE); String hadoopConfDir = System.getenv(HADOOP_CONFDIR_ENV_NAME); if (cmdline.hasOption(HADOOP_CONF_DIR)) { hadoopConfDir = cmdline.getOptionValue(HADOOP_CONF_DIR); } boolean distributed = hadoopConfDir != null && (mode == null || mode.equals(MODE_DISTRIBUTED)); if (MODE_DISTRIBUTED.equalsIgnoreCase(mode) && !distributed) { LOG.error("Cannot run in distributed mode. HADOOP_CONF_DIR is " + "not configured."); } if (LOG.isDebugEnabled()) { LOG.debug("Running in: " + (distributed ? "distributed " : "local") + "mode"); if (distributed) { LOG.debug("HADOOP_CONF_DIR is set to " + hadoopConfDir); } } conf.set(EXECUTION_MODE, distributed ? MODE_DISTRIBUTED : MODE_LOCAL); if (distributed) { if (!cmdline.hasOption(SPLIT_INPUT) && Command.getInputType(cmdline).equals(InputType.DELIMITED_TEXT)) { conf.setBoolean(ConfigConstants.CONF_SPLIT_INPUT, true); } File hdConfDir = new File(hadoopConfDir); try { checkHadoopConfDir(hdConfDir); } catch (IllegalArgumentException e) { LOG.error("Error found with Hadoop home setting", e); System.err.println(e.getMessage()); return 1; } // set new class loader based on Hadoop Conf Dir try { setClassLoader(hdConfDir, conf); } catch (Exception e) { LOG.error("Error configuring class loader", e); System.err.println(e.getMessage()); return 1; } } else { // running in local mode // Tell Hadoop that we are running in local mode. This is useful // when the user has Hadoop home or their Hadoop conf dir in their // classpath but want to run in local mode. conf.set(CONF_MAPREDUCE_JOBTRACKER_ADDRESS, "local"); } // create job Job job = null; try { if (distributed) { // So far all jobs created by mlcp are map only, // so set number of reduce tasks to 0. conf.setInt("mapreduce.job.reduces", 0); // No speculative runs since speculative tasks don't get to // clean up sessions properly conf.setBoolean("mapreduce.map.speculative", false); } else { // set working directory conf.set(CONF_MAPREDUCE_JOB_WORKING_DIR, System.getProperty("user.dir")); } job = command.createJob(conf, cmdline); } catch (Exception e) { // Print exception message. e.printStackTrace(); return 1; } LOG.info("Job name: " + job.getJobName()); // run job try { if (distributed) { // submit job submitJob(job); } else { runJobLocally(job, cmdline, command); } return 0; } catch (Exception e) { LOG.error("Error running a ContentPump job", e); e.printStackTrace(System.err); return 1; } }
From source file:com.navercorp.pinpoint.common.buffer.AutomaticBufferTest.java
@Test public void testPadBytes() throws Exception { int TOTAL_LENGTH = 20; int TEST_SIZE = 10; Buffer buffer = new AutomaticBuffer(10); byte[] test = new byte[10]; random.nextBytes(test);// www. j a v a2 s . com buffer.putPadBytes(test, TOTAL_LENGTH); byte[] result = buffer.getBuffer(); org.junit.Assert.assertEquals(result.length, TOTAL_LENGTH); org.junit.Assert.assertTrue("check data", Arrays.equals(Arrays.copyOfRange(test, 0, TEST_SIZE), Arrays.copyOfRange(result, 0, TEST_SIZE))); byte[] padBytes = new byte[TOTAL_LENGTH - TEST_SIZE]; org.junit.Assert.assertTrue("check pad", Arrays.equals(Arrays.copyOfRange(padBytes, 0, TEST_SIZE), Arrays.copyOfRange(result, TEST_SIZE, TOTAL_LENGTH))); }
From source file:my.adam.smo.EncryptionTest.java
@Test public void symetricEncryptionShortMessage() { ApplicationContext clientContext = new ClassPathXmlApplicationContext("Context.xml"); SymmetricEncryptionBox box = clientContext.getBean(SymmetricEncryptionBox.class); int plainTextLength = 3; byte[] plainText = new byte[plainTextLength]; random.nextBytes(plainText);/*from ww w . j a v a 2s .c o m*/ byte[] cryptogram = box.encrypt(plainText); Assert.assertFalse("plain text leaked!!!", Arrays.equals(plainText, Arrays.copyOfRange(cryptogram, SymmetricEncryptionBox.ivLength, cryptogram.length))); byte[] decrypted = box.decrypt(cryptogram); Assert.assertTrue("unable to decrypt", Arrays.equals(plainText, decrypted)); }