List of usage examples for java.util Arrays copyOfRange
public static boolean[] copyOfRange(boolean[] original, int from, int to)
From source file:de.fu_berlin.inf.dpp.intellij.project.fs.PathImp.java
@Override public IPath removeFirstSegments(int count) { String[] result = Arrays.copyOfRange(segments, count, segments.length); return new PathImp(join(result)); }
From source file:com.blockwithme.longdb.leveldb.Util.java
/** Index of. * /*from w w w. j av a 2 s . co m*/ * @param theColIds * the all column ids * @param theId * the id * @return the long */ public static long indexOf(final byte[] theColIds, final long theId) { int offset = 0; while (offset + LONG_BYTES < theColIds.length) { if (toLong(Arrays.copyOfRange(theColIds, offset, (offset + LONG_BYTES))) == theId) return offset; offset += LONG_BYTES; } return -1L; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * You should almost never use this. Instead, use the write* methods to * avoid copies.//w w w . j a v a2 s . c o m */ public static byte[] getArray(ByteBuffer buffer) { int length = buffer.remaining(); if (buffer.hasArray()) { int boff = buffer.arrayOffset() + buffer.position(); if (boff == 0 && length == buffer.array().length) return buffer.array(); else return Arrays.copyOfRange(buffer.array(), boff, boff + length); } // else, DirectByteBuffer.get() is the fastest route byte[] bytes = new byte[length]; buffer.duplicate().get(bytes); return bytes; }
From source file:com.titankingdoms.dev.titanchat.command.defaults.WhitelistCommand.java
@Override public void execute(CommandSender sender, Channel channel, String[] args) { if (channel == null) { sendMessage(sender, "&4Channel not defined"); return;//from w w w . j ava 2s.c om } if (!channel.getConfig().getBoolean("whitelist", false)) { sendMessage(sender, "&4Whitelist is not enabled for the channel"); return; } if (args[0].equalsIgnoreCase("list")) { String list = StringUtils.join(channel.getWhitelist(), ", "); sendMessage(sender, "&6" + channel.getName() + " Whitelist: " + list); return; } else { if (args.length < 2) { sendMessage(sender, "&4Invalid argument length"); String usage = "/titanchat [@<channel>] whitelist " + getUsage(); sendMessage(sender, "&6" + usage); return; } } Participant participant = plugin.getParticipantManager().getParticipant(args[1]); if (args[0].equalsIgnoreCase("add")) { if (channel.getWhitelist().contains(participant.getName())) { sendMessage(sender, participant.getDisplayName() + " &4is already on the whitelist"); return; } channel.getWhitelist().add(participant.getName()); participant.notice("&6You have been added to the whitelist of " + channel.getName()); if (args.length > 2) { String reason = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ").trim(); participant.notice("&6Reason: " + reason); } if (!channel.isLinked(plugin.getParticipantManager().getParticipant(sender))) sendMessage(sender, participant.getDisplayName() + " &6has been added to the whitelist"); channel.notice(participant.getDisplayName() + " &6has been added to the whitelist"); } else if (args[0].equalsIgnoreCase("remove")) { if (!channel.getWhitelist().contains(participant.getName())) { sendMessage(sender, participant.getDisplayName() + " &4is not on the whitelist"); return; } channel.getWhitelist().remove(participant.getName()); participant.notice("&4You have been removed from the whitelisted of " + channel.getName()); if (args.length > 2) { String reason = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " ").trim(); participant.notice("&4Reason: " + reason); } if (!channel.isLinked(plugin.getParticipantManager().getParticipant(sender))) sendMessage(sender, participant.getDisplayName() + " &6has been removed from the whitelist"); channel.notice(participant.getDisplayName() + " &6has been removed from the whitelist"); } else { sendMessage(sender, "&4Incorrect usage: /titanchat [@<channel>] whitelist " + getUsage()); } }
From source file:com.bconomy.autobit.Encryption.java
private static byte[] charsToBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data Arrays.fill(chars, '\u0000'); // clear sensitive data return bytes; }
From source file:com.codestation.henkakuserver.HenkakuServer.java
private Pair<ArrayList<Integer>, List<Byte>> preprocessRop(byte[] urop) throws Exception { byte[] loader = new byte[urop.length + ((-urop.length) & 3)]; System.arraycopy(urop, 0, loader, 0, urop.length); ByteBuffer buf = ByteBuffer.wrap(loader).order(ByteOrder.LITTLE_ENDIAN); int header_size = 0x40; int dsize = buf.getInt(0x10); int csize = buf.getInt(0x20); int reloc_size = buf.getInt(0x30); int symtab_size = buf.getInt(0x38); if (csize % 4 != 0) { throw new Exception("csize % 4 != 0???"); }/*from www . j a v a 2s . com*/ int reloc_offset = header_size + dsize + csize; int symtab = reloc_offset + reloc_size; int symtab_n = symtab_size / 8; Map<Integer, String> reloc_map = new HashMap<>(); for (int x = 0; x < symtab_n; ++x) { int sym_id = buf.getInt(symtab + 8 * x); int str_offset = buf.getInt(symtab + 8 * x + 4); int end = str_offset; while (loader[end] != 0) { end += 1; } String name = new String(Arrays.copyOfRange(loader, str_offset, end)); reloc_map.put(sym_id, name); } Map<Pair<String, Integer>, Integer> reloc_type_map = new HashMap<>(); reloc_type_map.put(new Pair<>("rop.data", 0), 1); reloc_type_map.put(new Pair<>("SceWebKit", 0), 2); reloc_type_map.put(new Pair<>("SceLibKernel", 0), 3); reloc_type_map.put(new Pair<>("SceLibc", 0), 4); reloc_type_map.put(new Pair<>("SceLibHttp", 0), 5); reloc_type_map.put(new Pair<>("SceNet", 0), 6); reloc_type_map.put(new Pair<>("SceAppMgr", 0), 7); int want_len = 0x40 + dsize + csize; ArrayList<Integer> urop_js = new ArrayList<>(); byte[] relocs = new byte[want_len / 4]; int reloc_n = reloc_size / 8; for (int x = 0; x < reloc_n; ++x) { int reloc_type = buf.getShort(reloc_offset + 8 * x); int sym_id = buf.getShort(reloc_offset + 8 * x + 2); int offset = buf.getInt(reloc_offset + 8 * x + 4); if (offset % 4 != 0) { throw new Exception("offset % 4 != 0???"); } if (relocs[offset / 4] != 0) { throw new Exception("symbol relocated twice, not supported"); } Integer wk_reloc_type = reloc_type_map.get(new Pair<>(reloc_map.get(sym_id), reloc_type)); if (wk_reloc_type == null) { throw new Exception("unsupported relocation type"); } relocs[offset / 4] = wk_reloc_type.byteValue(); } for (int x = 0; x < want_len; x += 4) { urop_js.add(buf.getInt(x)); } List<Byte> relocsArray = Arrays.asList(ArrayUtils.toObject(relocs)); return new Pair<>(urop_js, relocsArray); }
From source file:com.semsaas.utils.anyurl.App.java
private static String[] processOption(String[] args, Properties props) { LongOpt[] options = new LongOpt[] { new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 'o') }; // Build auxilary structures HashMap<Integer, LongOpt> shortOptionMap = new HashMap<Integer, LongOpt>(); StringBuffer decl = new StringBuffer(); for (LongOpt o : options) { shortOptionMap.put(o.getVal(), o); decl.append((char) o.getVal()); if (o.getHasArg() == LongOpt.OPTIONAL_ARGUMENT) { decl.append("::"); } else if (o.getHasArg() == LongOpt.REQUIRED_ARGUMENT) { decl.append(":"); }//from w w w .j a v a 2 s.c o m } Getopt g = new Getopt("anyurl", args, decl.toString(), options); int c = 0; while ((c = g.getopt()) != -1) { LongOpt opt = shortOptionMap.get(c); String optName = opt.getName(); String optVal = g.getOptarg(); props.put(optName, optVal); } // NB: Getopt moves non options to the end return Arrays.copyOfRange(args, g.getOptind(), args.length); }
From source file:com.opengamma.analytics.financial.model.finitedifference.ExtendedCoupledFiniteDifference.java
public PDEFullResults1D[] solve(final ExtendedCoupledPDEDataBundle pdeData1, final ExtendedCoupledPDEDataBundle pdeData2, final PDEGrid1D grid, final BoundaryCondition lowerBoundary1, final BoundaryCondition upperBoundary1, final BoundaryCondition lowerBoundary2, final BoundaryCondition upperBoundary2, @SuppressWarnings("unused") final Surface<Double, Double, Double> freeBoundary) { Validate.notNull(pdeData1, "pde1 data"); Validate.notNull(pdeData2, "pde2 data"); final int tNodes = grid.getNumTimeNodes(); final int xNodes = grid.getNumSpaceNodes(); final double theta = getTheta(); final Decomposition<?> dcomp = getDecomposition(); double[] f = new double[2 * xNodes]; final double[][] full1 = new double[tNodes][xNodes]; final double[][] full2 = new double[tNodes][xNodes]; final double[] q = new double[2 * xNodes]; final double[][] m = new double[2 * xNodes][2 * xNodes]; final double[][] a1 = new double[2][xNodes - 2]; final double[][] a2 = new double[2][xNodes - 2]; final double[][] b1 = new double[2][xNodes - 2]; final double[][] b2 = new double[2][xNodes - 2]; final double[][] c1 = new double[2][xNodes - 2]; final double[][] c2 = new double[2][xNodes - 2]; final double[][] alpha1 = new double[2][xNodes]; final double[][] alpha2 = new double[2][xNodes]; final double[][] beta1 = new double[2][xNodes]; final double[][] beta2 = new double[2][xNodes]; final double lambda1 = pdeData1.getCoupling(); final double lambda2 = pdeData2.getCoupling(); // final double omega = 1.5; // final int oldCount = 0; // final boolean omegaIncrease = false; double dt, t1, t2, x; double[] x1st, x2nd; for (int i = 0; i < xNodes; i++) { f[i] = pdeData1.getInitialCondition(grid.getSpaceNode(i)); }//from w w w. j ava 2 s .c o m for (int i = 0; i < xNodes; i++) { f[i + xNodes] = pdeData2.getInitialCondition(grid.getSpaceNode(i)); } full1[0] = Arrays.copyOfRange(f, 0, xNodes); full2[0] = Arrays.copyOfRange(f, xNodes, 2 * xNodes); for (int i = 0; i < xNodes - 2; i++) { x = grid.getSpaceNode(i + 1); a1[0][i] = pdeData1.getA(0, x); b1[0][i] = pdeData1.getB(0, x); c1[0][i] = pdeData1.getC(0, x); a1[1][i] = pdeData2.getA(0, x); b1[1][i] = pdeData2.getB(0, x); c1[1][i] = pdeData2.getC(0, x); } for (int i = 0; i < xNodes; i++) { x = grid.getSpaceNode(i); alpha1[0][i] = pdeData1.getAlpha(0, x); beta1[0][i] = pdeData1.getBeta(0, x); alpha1[1][i] = pdeData2.getAlpha(0, x); beta1[1][i] = pdeData2.getBeta(0, x); } final boolean first = true; DecompositionResult decompRes = null; for (int n = 1; n < tNodes; n++) { t1 = grid.getTimeNode(n - 1); t2 = grid.getTimeNode(n); dt = grid.getTimeStep(n - 1); for (int i = 0; i < xNodes; i++) { x = grid.getSpaceNode(i); alpha2[0][i] = pdeData1.getAlpha(t2, x); beta2[0][i] = pdeData1.getBeta(t2, x); alpha2[1][i] = pdeData2.getAlpha(t2, x); beta2[1][i] = pdeData2.getBeta(t2, x); } for (int i = 1; i < xNodes - 1; i++) { x = grid.getSpaceNode(i); x1st = grid.getFirstDerivativeCoefficients(i); x2nd = grid.getSecondDerivativeCoefficients(i); q[i] = f[i]; q[i] -= (1 - theta) * dt * (x2nd[0] * a1[0][i - 1] * alpha1[0][i - 1] + x1st[0] * b1[0][i - 1] * beta1[0][i - 1]) * f[i - 1]; q[i] -= (1 - theta) * dt * (x2nd[1] * a1[0][i - 1] * alpha1[0][i] + x1st[1] * b1[0][i - 1] * beta1[0][i] + c1[0][i - 1]) * f[i]; q[i] -= (1 - theta) * dt * (x2nd[2] * a1[0][i - 1] * alpha1[0][i + 1] + x1st[2] * b1[0][i - 1] * beta1[0][i + 1]) * f[i + 1]; q[i] -= (1 - theta) * dt * lambda1 * f[i + xNodes]; q[xNodes + i] = f[xNodes + i]; q[xNodes + i] -= (1 - theta) * dt * (x2nd[0] * a1[1][i - 1] * alpha1[1][i - 1] + x1st[0] * b1[1][i - 1] * beta1[1][i - 1]) * f[xNodes + i - 1]; q[xNodes + i] -= (1 - theta) * dt * (x2nd[1] * a1[1][i - 1] * alpha1[1][i] + x1st[1] * b1[1][i - 1] * beta1[1][i] + c1[1][i - 1]) * f[xNodes + i]; q[xNodes + i] -= (1 - theta) * dt * (x2nd[2] * a1[1][i - 1] * alpha1[1][i + 1] + x1st[2] * b1[1][i - 1] * beta1[1][i + 1]) * f[xNodes + i + 1]; q[xNodes + i] -= (1 - theta) * dt * lambda2 * f[i]; a2[0][i - 1] = pdeData1.getA(t2, x); b2[0][i - 1] = pdeData1.getB(t2, x); c2[0][i - 1] = pdeData1.getC(t2, x); a2[1][i - 1] = pdeData2.getA(t2, x); b2[1][i - 1] = pdeData2.getB(t2, x); c2[1][i - 1] = pdeData2.getC(t2, x); m[i][i - 1] = theta * dt * (x2nd[0] * a2[0][i - 1] * alpha2[0][i - 1] + x1st[0] * b2[0][i - 1] * beta2[0][i - 1]); m[i][i] = 1 + theta * dt * (x2nd[1] * a2[0][i - 1] * alpha2[0][i] + x1st[1] * b2[0][i - 1] * beta2[0][i] + c2[0][i - 1]); m[i][i + 1] = theta * dt * (x2nd[2] * a2[0][i - 1] * alpha2[0][i + 1] + x1st[2] * b2[0][i - 1] * beta2[0][i + 1]); m[i][i + xNodes] = dt * theta * lambda1; m[xNodes + i][xNodes + i - 1] = theta * dt * (x2nd[0] * a2[1][i - 1] * alpha2[1][i - 1] + x1st[0] * b2[1][i - 1] * beta2[1][i - 1]); m[xNodes + i][xNodes + i] = 1 + theta * dt * (x2nd[1] * a2[1][i - 1] * alpha2[1][i] + x1st[1] * b2[1][i - 1] * beta2[1][i] + c2[1][i - 1]); m[xNodes + i][xNodes + i + 1] = theta * dt * (x2nd[2] * a2[1][i - 1] * alpha2[1][i + 1] + x1st[2] * b2[1][i - 1] * beta2[1][i + 1]); m[xNodes + i][i] = dt * theta * lambda2; } double[] temp = lowerBoundary1.getLeftMatrixCondition(null, grid, t2); for (int k = 0; k < temp.length; k++) { m[0][k] = temp[k]; } temp = upperBoundary1.getLeftMatrixCondition(null, grid, t2); for (int k = 0; k < temp.length; k++) { m[xNodes - 1][xNodes - temp.length + k] = temp[k]; } temp = lowerBoundary2.getLeftMatrixCondition(null, grid, t2); for (int k = 0; k < temp.length; k++) { m[xNodes][xNodes + k] = temp[k]; } temp = upperBoundary2.getLeftMatrixCondition(null, grid, t2); for (int k = 0; k < temp.length; k++) { m[2 * xNodes - 1][2 * xNodes - temp.length + k] = temp[k]; } temp = lowerBoundary1.getRightMatrixCondition(null, grid, t1); double sum = 0; for (int k = 0; k < temp.length; k++) { sum += temp[k] * f[k]; } q[0] = sum + lowerBoundary1.getConstant(null, t2); temp = upperBoundary1.getRightMatrixCondition(null, grid, t1); sum = 0; for (int k = 0; k < temp.length; k++) { sum += temp[k] * f[xNodes - 1 - k]; } q[xNodes - 1] = sum + upperBoundary1.getConstant(null, t2); temp = lowerBoundary2.getRightMatrixCondition(null, grid, t1); sum = 0; for (int k = 0; k < temp.length; k++) { sum += temp[k] * f[k]; } q[xNodes] = sum + lowerBoundary2.getConstant(null, t2); temp = upperBoundary2.getRightMatrixCondition(null, grid, t1); sum = 0; for (int k = 0; k < temp.length; k++) { sum += temp[k] * f[xNodes - 1 - k]; } q[2 * xNodes - 1] = sum + upperBoundary2.getConstant(null, t2); if (first) { final DoubleMatrix2D mM = new DoubleMatrix2D(m); decompRes = dcomp.evaluate(mM); // first = false; } f = decompRes.solve(q); a1[0] = Arrays.copyOf(a2[0], xNodes - 2); b1[0] = Arrays.copyOf(b2[0], xNodes - 2); c1[0] = Arrays.copyOf(c2[0], xNodes - 2); alpha1[0] = Arrays.copyOf(alpha2[0], xNodes); beta1[0] = Arrays.copyOf(beta2[0], xNodes); a1[1] = Arrays.copyOf(a2[1], xNodes - 2); b1[1] = Arrays.copyOf(b2[1], xNodes - 2); c1[1] = Arrays.copyOf(c2[1], xNodes - 2); alpha1[1] = Arrays.copyOf(alpha2[1], xNodes); beta1[1] = Arrays.copyOf(beta2[1], xNodes); full1[n] = Arrays.copyOfRange(f, 0, xNodes); full2[n] = Arrays.copyOfRange(f, xNodes, 2 * xNodes); } final PDEFullResults1D[] res = new PDEFullResults1D[2]; res[0] = new PDEFullResults1D(grid, full1); res[1] = new PDEFullResults1D(grid, full2); return res; }
From source file:com.netflix.spinnaker.halyard.deploy.job.v1.JobExecutorLocal.java
@Override public String startJob(JobRequest jobRequest, Map<String, String> env, InputStream stdIn) { List<String> tokenizedCommand = jobRequest.getTokenizedCommand(); if (tokenizedCommand == null || tokenizedCommand.isEmpty()) { throw new IllegalArgumentException("JobRequest must include a tokenized command to run"); }// ww w . j a v a2 s.co m final long timeoutMillis = jobRequest.getTimeoutMillis() == null ? ExecuteWatchdog.INFINITE_TIMEOUT : jobRequest.getTimeoutMillis(); String jobId = UUID.randomUUID().toString(); log.info("Scheduling job " + jobRequest.getTokenizedCommand() + " with id " + jobId); scheduler.createWorker().schedule(new Action0() { @Override public void call() { ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdOut, stdErr, stdIn); CommandLine commandLine; log.info("Executing " + jobId + "with tokenized command: " + tokenizedCommand); // Grab the first element as the command. commandLine = new CommandLine(jobRequest.getTokenizedCommand().get(0)); // Treat the rest as arguments. String[] arguments = Arrays.copyOfRange(tokenizedCommand.toArray(new String[0]), 1, tokenizedCommand.size()); commandLine.addArguments(arguments, false); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutMillis) { @Override public void timeoutOccured(Watchdog w) { // If a watchdog is passed in, this was an actual time-out. Otherwise, it is likely // the result of calling watchdog.destroyProcess(). if (w != null) { log.warn("Job " + jobId + " timed-out after " + timeoutMillis + "ms."); cancelJob(jobId); } super.timeoutOccured(w); } }; Executor executor = new DefaultExecutor(); executor.setStreamHandler(pumpStreamHandler); executor.setWatchdog(watchdog); try { executor.execute(commandLine, env, resultHandler); } catch (IOException e) { throw new RuntimeException("Execution of " + jobId + " failed ", e); } // Give the job some time to spin up. try { Thread.sleep(500); } catch (InterruptedException e) { } jobIdToHandlerMap.put(jobId, new ExecutionHandler().setResultHandler(resultHandler) .setWatchdog(watchdog).setStdOut(stdOut).setStdErr(stdErr)); } }); return jobId; }
From source file:bb.mcmc.analysis.HeidelbergConvergeStat.java
@Override protected double calculateEachStat(String key) { double[] t = traceValues.get(key); t = Arrays.copyOfRange(t, 0, 48); int length = t.length; System.out.println(length + "\t" + Arrays.toString(t) + "\t" + length); // length=13578; // t = new double[length]; int indexStart = (int) Math.ceil(length / 2.0 - 1); // System.out.println(length +"\t"+ indexStart); double[] Y = Arrays.copyOfRange(t, indexStart, length); double S0 = ConvergeStatUtils.spectrum0(Y); System.out.println(Y.length + "\t" + indexStart + "\t" + S0 + "\t" + Arrays.toString(Y)); final double oneTenthLength = length / 10.0; for (int i = 0; i < 5; i++) { int startVec = (int) Math.ceil(0 + i * oneTenthLength); Y = Arrays.copyOfRange(t, startVec, length); int n = Y.length; double ybar = DiscreteStatistics.mean(Y); double nS0 = n * S0; double[] B = new double[n]; double[] Bsq = new double[n]; B[0] = Y[0] - ybar;/*from w ww.java 2s .c om*/ for (int j = 1; j < B.length; j++) { B[j] = B[j - 1] + Y[j] - ybar; } for (int j = 0; j < Bsq.length; j++) { Bsq[j] = B[j] * B[j] / nS0; } double ind = StatUtils.sum(Bsq) / n; // Bsq <- (B * B)/(n * S0) System.out.println(n + "\t" + S0 + "\t" + StatUtils.sum(B) + "\t" + StatUtils.sum(Bsq) + "\t" + ind); CramerVonMisesDist dist = new CramerVonMisesDist(2); System.out.println(dist.barF(ind)); System.out.println(dist.cdf(ind)); System.out.println(dist.density(ind)); System.out.println(dist.density(ind)); // The Cramer-von Mises Distribution boolean ind2 = pcramer(ind); // TODO: if (ind2) { break; } // int output_list[] = new int[input_list.length]; // long startTime = System.currentTimeMillis(); // for (int i = 1; i < input_list.length; i++) // output_list[i] = output_list[i-1] + input_list[i]; // long endTime = System.currentTimeMillis(); // System.out.println(Y.length +"\t"+ ybar); // for (i in seq(along = start.vec)) { // Y <- window(Y, start = start.vec[i]) // n <- niter(Y) // ybar <- mean(Y) // B <- cumsum(Y) - ybar * (1:n) // Bsq <- (B * B)/(n * S0) // I <- sum(Bsq)/n // if (converged <- !is.na(I) && pcramer(I) < 1 - pvalue) // break // } } double S0ci = ConvergeStatUtils.spectrum0(Y); double halfwidth = 1.96 * Math.sqrt(S0ci / Y.length); // System.out.println(oneTenthLength +"\t"+ Arrays.toString(startVec)); // final double[] t = traceValues.get(key); // final int length = t.length; // final int indexEnd = (int) Math.ceil(length * frac1); // // final double[] dStart = Arrays.copyOfRange(t, 0, indexEnd); // final double[] dEnd = Arrays.copyOfRange(t, indexStart, length); // return 0; }