List of usage examples for org.apache.commons.lang ArrayUtils subarray
public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive)
Produces a new boolean
array containing the elements between the start and end indices.
From source file:edu.utdallas.bigsecret.crypter.CrypterMode3.java
/** * {@inheritDoc}//from w w w .j a v a2 s . c om */ public byte[] unwrapRow(byte[] row, byte[] family, byte[] qualifier, long ts, byte[] value) throws Exception { if (qualifier == null || qualifier.length == 0) throw new Exception("Qualifier data null or no data"); byte[] completeData = m_keyCipher.decrypt(qualifier); int rowSize = Bytes.toInt(completeData, 0, 4); return ArrayUtils.subarray(completeData, 12, 12 + rowSize); }
From source file:de.codesourcery.jasm16.compiler.io.FileObjectCodeWriterTest.java
public void testSkipToOffsetAfterWrite() throws Exception { final byte[] input = "test".getBytes(); final int offset = 8; writer.writeObjectCode(input);//from ww w . j a va 2 s.c o m writer.advanceToWriteOffset(Address.byteAddress(offset)); writer.writeObjectCode(input); assertEquals(Address.byteAddress(offset + input.length), writer.getCurrentWriteOffset()); writer.close(); final byte[] data = writer.getBytes(); System.out.println(Misc.toHexDumpWithAddresses(Address.byteAddress(offset), data, 8)); assertEquals(offset + input.length, data.length); byte[] zeros = new byte[4]; assertTrue(ArrayUtils.isEquals(zeros, ArrayUtils.subarray(data, input.length, offset))); assertTrue(ArrayUtils.isEquals(input, ArrayUtils.subarray(data, offset, data.length))); }
From source file:com.github.neoio.nio.util.NIOUtils.java
public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel) throws NetIOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {// w ww. j a v a 2 s .co m logger.debug("bytes read from channel: " + IOUtils.copy(Channels.newInputStream(channel), bos)); } catch (IOException e) { throw new NetIOException(e); } return ByteBuffer.wrap(ArrayUtils.subarray(bos.toByteArray(), 0, bos.size())); }
From source file:app.Des.java
private List<int[]> doKey() { int[] keyPermuted = this.tables.getPc1_Key(this.key); int[] keyL = ArrayUtils.subarray(keyPermuted, 0, keyPermuted.length / 2); int[] keyR = ArrayUtils.subarray(keyPermuted, keyPermuted.length / 2, keyPermuted.length); List<int[]> subkeysL = new ArrayList<>(); List<int[]> subkeysR = new ArrayList<>(); int[] lastL = Arrays.copyOf(keyL, keyL.length); int[] lastR = Arrays.copyOf(keyR, keyR.length); for (int shift : this.tables.getShiftKeyTable()) { int[] newKeyL = this.binMath.doLeftShift(lastL, shift); int[] newKeyR = this.binMath.doLeftShift(lastR, shift); subkeysL.add(newKeyL);//from w ww . j ava2 s .c o m subkeysR.add(newKeyR); lastL = newKeyL; lastR = newKeyR; } List<int[]> mergedAndPermuted = new ArrayList<>(); for (int i = 0; i < subkeysL.size(); i++) { int[] tempKey = new int[this.B_56]; tempKey = ArrayUtils.clone(subkeysL.get(i)); tempKey = ArrayUtils.addAll(tempKey, subkeysR.get(i)); mergedAndPermuted.add(this.tables.getPc2_Key(tempKey)); } return mergedAndPermuted; }
From source file:com.intel.daal.spark.rdd.DistributedNumericTable.java
/** * Split a DistributedNumericTable into a pair JavaPairRDD<NumericTable, NumericTable>. * @param distNT - The DistributedNumericTable to be split. * @param position - The column index at which to split. * @return A JavaPairRDD that contains a pair of NumericTables. * @throws IllegalArgumentException//from w ww . j a v a2s . c o m */ public static JavaPairRDD<NumericTable, NumericTable> split(DistributedNumericTable distNT, final int position) throws IllegalArgumentException { return distNT.numTables.mapToPair(new PairFunction<NumericTableWithIndex, NumericTable, NumericTable>() { public Tuple2<NumericTable, NumericTable> call(NumericTableWithIndex nt) { DaalContext context = new DaalContext(); NumericTable table = nt.getTable(context); double[] data = (table instanceof HomogenNumericTable) ? ((HomogenNumericTable) table).getDoubleArray() : null; if (data == null) { throw new IllegalArgumentException("Invalid NumericTable type"); } long begin = 0; long end = nt.numOfCols(); int firstNumCols = position; int secondNumCols = (int) nt.numOfCols() - position; double[] first = new double[(int) nt.numOfRows() * firstNumCols]; double[] second = new double[(int) nt.numOfRows() * secondNumCols]; int firstBegin = 0; int secondBegin = 0; while (begin < data.length) { double[] row = ArrayUtils.subarray(data, (int) begin, (int) end); System.arraycopy(row, 0, first, firstBegin, position); firstBegin += position; System.arraycopy(row, position, second, secondBegin, secondNumCols); secondBegin += secondNumCols; begin = end; end += nt.numOfCols(); } HomogenNumericTable t1 = new HomogenNumericTable(context, first, position, nt.numOfRows()); t1.pack(); HomogenNumericTable t2 = new HomogenNumericTable(context, second, nt.numOfCols() - position, nt.numOfRows()); t2.pack(); Tuple2<NumericTable, NumericTable> tup = new Tuple2<NumericTable, NumericTable>(t1, t2); context.dispose(); return tup; } }); }
From source file:alluxio.cli.AlluxioShell.java
/** * Handles the specified shell command request, displaying usage if the command format is invalid. * * @param argv [] Array of arguments given by the user's input from the terminal * @return 0 if command is successful, -1 if an error occurred *//* ww w.j a v a2 s.co m*/ public int run(String... argv) { if (argv.length == 0) { printUsage(); return -1; } // Sanity check on the number of arguments String cmd = argv[0]; ShellCommand command = mCommands.get(cmd); if (command == null) { // Unknown command (we didn't find the cmd in our dict) String[] replacementCmd = getReplacementCmd(cmd); if (replacementCmd == null) { System.out.println(cmd + " is an unknown command.\n"); printUsage(); return -1; } // Handle command alias, and print out WARNING message for deprecated cmd. String deprecatedMsg = "WARNING: " + cmd + " is deprecated. Please use " + StringUtils.join(replacementCmd, " ") + " instead."; System.out.println(deprecatedMsg); LOG.warn(deprecatedMsg); String[] replacementArgv = (String[]) ArrayUtils.addAll(replacementCmd, ArrayUtils.subarray(argv, 1, argv.length)); return run(replacementArgv); } String[] args = Arrays.copyOfRange(argv, 1, argv.length); CommandLine cmdline = command.parseAndValidateArgs(args); if (cmdline == null) { printUsage(); return -1; } // Handle the command try { return command.run(cmdline); } catch (Exception e) { System.out.println(e.getMessage()); LOG.error("Error running " + StringUtils.join(argv, " "), e); return -1; } }
From source file:net.jolm.util.UserPasswordHelper.java
/** * Verifies a given password against the password stored in the userPassword * field in LDAP.. <p/> The userPassword-value should follow the following * format: - {MD5}base64(MD5-hash) - {SHA}base64(SHA-hash) - * {SMD5}base64(MD5-hash+salt bytes) - {SSHA}base64(SHA-hash+salt bytes) - * plaintext password <p/> If the userPassword value does not start with one * of the prefixes {MD5}, {SMD5}, {SHA} or {SSHA} it will be handled as a * plaintext pwd. <p/>/*from ww w . j a v a2 s . c om*/ * * @param clearpass * The password in plaintext that should be verified against the * hashed pwd stored in the userPassword * @param userPassword * The original pwd stored in the userPassword value. field. * @return True - if the given plaintext pwd matches with the hashed pwd in * the userPassword field, otherwise false. */ public static boolean verifyPassword(String clearpass, byte[] userPassword) { try { String hashFromClearText = null; String userPasswordInText = new String(userPassword, DEFAULT_ENCODING); if (userPasswordInText.startsWith("{MD5}")) { hashFromClearText = new String(clearPassToUserPassword(clearpass, HashAlg.MD5, null)); } else if (userPasswordInText.startsWith("{SMD5}")) { byte[] hashPlusSalt = Base64 .decodeBase64(userPasswordInText.substring(6).getBytes(DEFAULT_ENCODING)); // The length of an MD5-hash is always 16 Bytes. To get // the size of the salt we need to subtract 16 Bytes from // the total length of the base64-decoded String. byte[] salt = ArrayUtils.subarray(hashPlusSalt, 16, hashPlusSalt.length); hashFromClearText = new String(clearPassToUserPassword(clearpass, HashAlg.SMD5, salt)); } else if (userPasswordInText.startsWith("{SHA}")) { hashFromClearText = new String(clearPassToUserPassword(clearpass, HashAlg.SHA, null)); } else if (userPasswordInText.startsWith("{SSHA}")) { byte[] hashPlusSalt = Base64 .decodeBase64(userPasswordInText.substring(6).getBytes(DEFAULT_ENCODING)); // The length of an SHA-1-hash is always 20 Bytes. To get // the size of the salt we need to subtract 20 Bytes from // the total length of the base64-decoded String. byte[] salt = ArrayUtils.subarray(hashPlusSalt, 20, hashPlusSalt.length); hashFromClearText = new String(clearPassToUserPassword(clearpass, HashAlg.SSHA, salt)); } else { hashFromClearText = clearpass; } return hashFromClearText.equals(userPasswordInText); } catch (UnsupportedEncodingException uee) { log.warn("Error occurred while verifying password", uee); return false; } }
From source file:gda.jython.accesscontrol.ProtectedMethodComponent.java
/** * Tests if the method is an invoke method from PyObject. If so, runs the method else throws a * MethodNotPyObjectInvokeException.//from w w w . ja va2 s. c om * <p> * have a SuppressWarnings beacuase of call to Py.tojava(). This method is not really deprecated: its a typo in the * Jython code! * * @param obj * @param method * @param args * @return Object - result of the method call * @throws Throwable */ public Object testForJythonInvocation(Object obj, Method method, Object[] args) throws Throwable { // if we are calling one of the jtyhon invoke methods if (isAnInvokeMethod(method)) { // extract the method name String methodName = args[0].toString(); // extract the array of arguments Object[] newArgs = ArrayUtils.subarray(args, 1, args.length); // as the method is being called via a Jython invoke, we know that the arguments must be an array of // PyObjects. // if no arguments if (newArgs.length == 0) { return ((PyObject) obj).__getattr__(methodName).__call__(); } // if more than two arguments, then jython will have put them into an array of PyObjects else if (newArgs.length == 1 && newArgs[0] instanceof PyObject[]) { return ((PyObject) obj).__getattr__(methodName).__call__((PyObject[]) newArgs[0], Py.NoKeywords); } // if one argument then call the shortcut for one method (see Jython Javadoc) else if (newArgs.length == 1) { return ((PyObject) obj).__getattr__(methodName).__call__((PyObject) newArgs[0]); } // if two arguments then call the shortcut for one method (see Jython Javadoc) else if (newArgs.length == 2) { return ((PyObject) obj).__getattr__(methodName).__call__((PyObject) newArgs[0], (PyObject) newArgs[1]); } } // if get here so call method anyway return method.invoke(obj, args); }
From source file:com.haulmont.chile.core.model.utils.InstanceUtils.java
/** * Set value of an attribute according to the rules described in {@link Instance#setValueEx(String, Object)}. * @param instance instance//from w w w . j ava 2s. c o m * @param properties path to the attribute * @param value attribute value */ public static void setValueEx(Instance instance, String[] properties, Object value) { if (properties.length > 1) { String[] subarray = (String[]) ArrayUtils.subarray(properties, 0, properties.length - 1); String intermediatePath = formatValuePath(subarray); instance = instance.getValueEx(intermediatePath); if (instance != null) { String property = properties[properties.length - 1]; instance.setValue(property, value); } else { throw new IllegalStateException(String.format("Can't find property '%s' value", intermediatePath)); } } else { instance.setValue(properties[0], value); } }
From source file:net.bible.android.view.activity.base.actionbar.Title.java
private String[] reduceTo2Parts(String[] parts, boolean lastAreMoreSignificant) { // return the last 2 parts as only show 2 and last are normally most significant if (lastAreMoreSignificant) { parts = ArrayUtils.subarray(parts, parts.length - 2, parts.length); } else {/* ww w .j ava 2s . c om*/ parts = ArrayUtils.subarray(parts, 0, 2); } return parts; }