List of usage examples for java.lang Integer SIZE
int SIZE
To view the source code for java.lang Integer SIZE.
Click Source Link
From source file:de.rwhq.btree.InnerNode.java
/** * @param rawKeys/* www . j a v a 2 s . co m*/ * @param pageIds * @param fromId * @return */ public int bulkInitialize(final ArrayList<byte[]> rawKeys, final ArrayList<Integer> pageIds, final int fromId) { if (pageIds.size() < (fromId + 2) || rawKeys.size() != (pageIds.size() - 1)) throw new IllegalArgumentException( "for bulkinsert, you must have at least 2 page ids and keys.size() == (pageIds.size() - 1)\n" + "pageIds.size()=" + pageIds.size() + ";fromId=" + fromId + ";rawKeys.size()=" + rawKeys.size()); final int fromId2 = fromId; initialize(); final ByteBuffer buf = rawPage().bufferForWriting(Header.size()); buf.putInt(pageIds.get(fromId2)); final int requiredSpace = Integer.SIZE / 8 + rawKeys.get(0).length; final int spaceForEntries = buf.remaining() / requiredSpace; final int totalEntriesToInsert = (pageIds.size() - fromId - 1); int entriesToInsert = spaceForEntries < totalEntriesToInsert ? spaceForEntries : totalEntriesToInsert; // make sure that not exactly one pageId remains, because that can't be inserted alone in the next // InnerNode. == 2 because final int remaining = pageIds.size() - fromId - (entriesToInsert + 1); if (remaining == 1) entriesToInsert--; for (int i = 0; i < entriesToInsert; i++) { // System.out.println("fetching rawKey " + (fromId + i) + " from array length " + rawKeys.size() + " with i=" + i); buf.put(rawKeys.get(fromId + i)); // fromId + 1 - 1 +i //LOG.debug("insert key: " + keySerializer.deserialize(rawKeys.get(fromId + i))); buf.putInt(pageIds.get(fromId + 1 + i)); } setNumberOfKeys(entriesToInsert); rawPage.sync(); return entriesToInsert + 1; // page ids }
From source file:com.android.nobug.view.pattern.PatternView.java
/** * Returns the greatest common divisor of {@code a, b}. Returns {@code 0} if * {@code a == 0 && b == 0}./*w ww . ja va 2 s . co m*/ * * @throws IllegalArgumentException if {@code a < 0} or {@code b < 0} */ private static int gcd(int a, int b) { /* * The reason we require both arguments to be >= 0 is because otherwise, what do you return * on gcd(0, Integer.MIN_VALUE)? BigInteger.gcd would return positive 2^31, but positive * 2^31 isn't an int. */ //checkNonNegative("a", a); if (a < 0) { throw new IllegalArgumentException("a (" + a + ") must be >= 0"); } //checkNonNegative("b", b); if (b < 0) { throw new IllegalArgumentException("b (" + b + ") must be >= 0"); } if (a == 0) { // 0 % b == 0, so b divides a, but the converse doesn't hold. // BigInteger.gcd is consistent with this decision. return b; } else if (b == 0) { return a; // similar logic } /* * Uses the binary GCD algorithm; see http://en.wikipedia.org/wiki/Binary_GCD_algorithm. * This is >40% faster than the Euclidean algorithm in benchmarks. */ int aTwos = Integer.numberOfTrailingZeros(a); a >>= aTwos; // divide out all 2s int bTwos = Integer.numberOfTrailingZeros(b); b >>= bTwos; // divide out all 2s while (a != b) { // both a, b are odd // The key to the binary GCD algorithm is as follows: // Both a and b are odd. Assume a > b; then gcd(a - b, b) = gcd(a, b). // But in gcd(a - b, b), a - b is even and b is odd, so we can divide out powers of two. // We bend over backwards to avoid branching, adapting a technique from // http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax int delta = a - b; // can't overflow, since a and b are nonnegative int minDeltaOrZero = delta & (delta >> (Integer.SIZE - 1)); // equivalent to Math.min(delta, 0) a = delta - minDeltaOrZero - minDeltaOrZero; // sets a to Math.abs(a - b) // a is now nonnegative and even b += minDeltaOrZero; // sets b to min(old a, b) a >>= Integer.numberOfTrailingZeros(a); // divide out all 2s, since 2 doesn't divide b } return a << Math.min(aTwos, bTwos); }
From source file:me.zhanghai.android.patternlock.PatternView.java
/** * Returns the greatest common divisor of {@code a, b}. Returns {@code 0} if * {@code a == 0 && b == 0}./*from w w w .ja va2 s . c o m*/ * * @throws IllegalArgumentException if {@code a < 0} or {@code b < 0} */ public static int gcd(int a, int b) { /* * The reason we require both arguments to be >= 0 is because otherwise, what do you return * on gcd(0, Integer.MIN_VALUE)? BigInteger.gcd would return positive 2^31, but positive * 2^31 isn't an int. */ //checkNonNegative("a", a); if (a < 0) { throw new IllegalArgumentException("a (" + a + ") must be >= 0"); } //checkNonNegative("b", b); if (b < 0) { throw new IllegalArgumentException("b (" + b + ") must be >= 0"); } if (a == 0) { // 0 % b == 0, so b divides a, but the converse doesn't hold. // BigInteger.gcd is consistent with this decision. return b; } else if (b == 0) { return a; // similar logic } /* * Uses the binary GCD algorithm; see http://en.wikipedia.org/wiki/Binary_GCD_algorithm. * This is >40% faster than the Euclidean algorithm in benchmarks. */ int aTwos = Integer.numberOfTrailingZeros(a); a >>= aTwos; // divide out all 2s int bTwos = Integer.numberOfTrailingZeros(b); b >>= bTwos; // divide out all 2s while (a != b) { // both a, b are odd // The key to the binary GCD algorithm is as follows: // Both a and b are odd. Assume a > b; then gcd(a - b, b) = gcd(a, b). // But in gcd(a - b, b), a - b is even and b is odd, so we can divide out powers of two. // We bend over backwards to avoid branching, adapting a technique from // http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax int delta = a - b; // can't overflow, since a and b are nonnegative int minDeltaOrZero = delta & (delta >> (Integer.SIZE - 1)); // equivalent to Math.min(delta, 0) a = delta - minDeltaOrZero - minDeltaOrZero; // sets a to Math.abs(a - b) // a is now nonnegative and even b += minDeltaOrZero; // sets b to min(old a, b) a >>= Integer.numberOfTrailingZeros(a); // divide out all 2s, since 2 doesn't divide b } return a << Math.min(aTwos, bTwos); }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> mediaSerialize(Cursor cursor) { logger.debug("::mediaSerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { MediaWrapper iw = new MediaWrapper(); iw.setEventId(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.EVENT_ID))); iw.setDataType(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA_TYPE))); iw.setData(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA))); iw.setCreatedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.CREATED_DATE))); iw.setModifiedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.MODIFIED_DATE))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(MediaTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try {//from w w w .j a v a 2s .c o m eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :event id eventId event_id\n try { String fileName = iw.getData(); File dataFile = new File(fileName); int dataSize = (int) dataFile.length(); byte[] buffData = new byte[dataSize]; FileInputStream fileStream = new FileInputStream(dataFile); int ret = 0; for (int position = 0; (ret > -1 && dataSize > position); position += ret) { ret = fileStream.read(buffData, position, dataSize - position); } fileStream.close(); eos.writeBytes("data"); eos.writeByte(0); ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN); dataSizeBuf.putInt(dataSize); // write the media back out eos.write(dataSizeBuf.array()); eos.write(buffData); eos.write(dataSizeBuf.array()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // not a reference field name :created date createdDate created_date\n // not a reference field name :modified date modifiedDate modified_date\n // MediaTableSchemaBase._DISPOSITION; // try { // TODO write to content provider using openFile // if (!applCacheMediaDir.exists() ) applCacheMediaDir.mkdirs(); // File outfile = new File(applCacheMediaDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:it.unimi.dsi.sux4j.mph.MWHCFunction.java
/** Returns the number of bits used by this structure. * /*from www . jav a 2 s. c o m*/ * @return the number of bits used by this structure. */ public long numBits() { if (n == 0) return 0; return (marker != null ? rank.numBits() + marker.length() : 0) + (data != null ? data.size64() : 0) * width + seed.length * (long) Long.SIZE + offset.length * (long) Integer.SIZE; }
From source file:org.eclipse.dataset.AbstractDataset.java
/** * @param dtype//from www. ja v a 2 s. co m * @param isize * number of elements in an item * @return length of single item in bytes */ public static int getItemsize(final int dtype, final int isize) { int size; switch (dtype) { case BOOL: size = 1; // How is this defined? break; case INT8: case ARRAYINT8: size = Byte.SIZE / 8; break; case INT16: case ARRAYINT16: case RGB: size = Short.SIZE / 8; break; case INT32: case ARRAYINT32: size = Integer.SIZE / 8; break; case INT64: case ARRAYINT64: size = Long.SIZE / 8; break; case FLOAT32: case ARRAYFLOAT32: case COMPLEX64: size = Float.SIZE / 8; break; case FLOAT64: case ARRAYFLOAT64: case COMPLEX128: size = Double.SIZE / 8; break; default: size = 0; break; } return size * isize; }
From source file:org.nd4j.linalg.util.ArrayUtil.java
/** * * @param intArray//from w ww . j a va 2 s .co m * @return */ public static byte[] toByteArray(int[] intArray) { int times = Integer.SIZE / Byte.SIZE; byte[] bytes = new byte[intArray.length * times]; for (int i = 0; i < intArray.length; i++) { ByteBuffer.wrap(bytes, i * times, times).putInt(intArray[i]); } return bytes; }
From source file:org.nd4j.linalg.util.ArrayUtil.java
/** * * @param byteArray//from w w w . jav a2s. co m * @return */ public static int[] toIntArray(byte[] byteArray) { int times = Integer.SIZE / Byte.SIZE; int[] ints = new int[byteArray.length / times]; for (int i = 0; i < ints.length; i++) { ints[i] = ByteBuffer.wrap(byteArray, i * times, times).getInt(); } return ints; }
From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java
public ArrayList<File> categorySerialize(Cursor cursor) { logger.debug("::categorySerialize"); ArrayList<File> paths = new ArrayList<File>(); if (1 > cursor.getCount()) return paths; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream eos = new DataOutputStream(baos); for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) { CategoryWrapper iw = new CategoryWrapper(); iw.setMainCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.MAIN_CATEGORY))); iw.setSubCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.SUB_CATEGORY))); iw.setTigrId(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.TIGR_ID))); iw.setIconType(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON_TYPE))); iw.setIcon(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON))); iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(CategoryTableSchemaBase._RECEIVED_DATE))); iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(CategoryTableSchemaBase._DISPOSITION))); Gson gson = new Gson(); try {/* www . ja v a 2s. com*/ eos.writeBytes(gson.toJson(iw)); eos.writeByte(0); } catch (IOException ex) { ex.printStackTrace(); } // not a reference field name :main category mainCategory main_category\n // not a reference field name :sub category subCategory sub_category\n // not a reference field name :tigr id tigrId tigr_id\n try { String fileName = iw.getIcon(); File dataFile = new File(fileName); int dataSize = (int) dataFile.length(); byte[] buffData = new byte[dataSize]; FileInputStream fileStream = new FileInputStream(dataFile); int ret = 0; for (int position = 0; (ret > -1 && dataSize > position); position += ret) { ret = fileStream.read(buffData, position, dataSize - position); } fileStream.close(); eos.writeBytes("icon"); eos.writeByte(0); ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN); dataSizeBuf.putInt(dataSize); // write the category back out eos.write(dataSizeBuf.array()); eos.write(buffData); eos.write(dataSizeBuf.array()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // CategoryTableSchemaBase._DISPOSITION; // try { // if (!applCacheCategoryDir.exists() ) applCacheCategoryDir.mkdirs(); // // File outfile = new File(applCacheCategoryDir, Integer.toHexString((int) System.currentTimeMillis())); // BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192); // bufferedOutput.write(baos.toByteArray()); // bufferedOutput.flush(); // bufferedOutput.close(); // // paths.add(outfile); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } return paths; }
From source file:org.apache.hadoop.hdfs.server.namenode.TestCheckpoint.java
/** * Tests save namespace.//w w w.j a v a2s . co m */ @Test public void testSaveNamespace() throws IOException { MiniDFSCluster cluster = null; DistributedFileSystem fs = null; FileContext fc; try { Configuration conf = new HdfsConfiguration(); cluster = new MiniDFSCluster.Builder(conf).numDataNodes(numDatanodes).format(true).build(); cluster.waitActive(); fs = (cluster.getFileSystem()); fc = FileContext.getFileContext(cluster.getURI(0)); // Saving image without safe mode should fail DFSAdmin admin = new DFSAdmin(conf); String[] args = new String[] { "-saveNamespace" }; try { admin.run(args); } catch (IOException eIO) { assertTrue(eIO.getLocalizedMessage().contains("Safe mode should be turned ON")); } catch (Exception e) { throw new IOException(e); } // create new file Path file = new Path("namespace.dat"); writeFile(fs, file, replication); checkFile(fs, file, replication); // create new link Path symlink = new Path("file.link"); fc.createSymlink(file, symlink, false); assertTrue(fc.getFileLinkStatus(symlink).isSymlink()); // verify that the edits file is NOT empty Collection<URI> editsDirs = cluster.getNameEditsDirs(0); for (URI uri : editsDirs) { File ed = new File(uri.getPath()); assertTrue(new File(ed, "current/" + NNStorage.getInProgressEditsFileName(1)) .length() > Integer.SIZE / Byte.SIZE); } // Saving image in safe mode should succeed fs.setSafeMode(SafeModeAction.SAFEMODE_ENTER); try { admin.run(args); } catch (Exception e) { throw new IOException(e); } // TODO: Fix the test to not require a hard-coded transaction count. final int EXPECTED_TXNS_FIRST_SEG = 13; // the following steps should have happened: // edits_inprogress_1 -> edits_1-12 (finalized) // fsimage_12 created // edits_inprogress_13 created // for (URI uri : editsDirs) { File ed = new File(uri.getPath()); File curDir = new File(ed, "current"); LOG.info("Files in " + curDir + ":\n " + Joiner.on("\n ").join(curDir.list())); // Verify that the first edits file got finalized File originalEdits = new File(curDir, NNStorage.getInProgressEditsFileName(1)); assertFalse(originalEdits.exists()); File finalizedEdits = new File(curDir, NNStorage.getFinalizedEditsFileName(1, EXPECTED_TXNS_FIRST_SEG)); GenericTestUtils.assertExists(finalizedEdits); assertTrue(finalizedEdits.length() > Integer.SIZE / Byte.SIZE); GenericTestUtils.assertExists(new File(ed, "current/" + NNStorage.getInProgressEditsFileName(EXPECTED_TXNS_FIRST_SEG + 1))); } Collection<URI> imageDirs = cluster.getNameDirs(0); for (URI uri : imageDirs) { File imageDir = new File(uri.getPath()); File savedImage = new File(imageDir, "current/" + NNStorage.getImageFileName(EXPECTED_TXNS_FIRST_SEG)); assertTrue("Should have saved image at " + savedImage, savedImage.exists()); } // restart cluster and verify file exists cluster.shutdown(); cluster = null; cluster = new MiniDFSCluster.Builder(conf).numDataNodes(numDatanodes).format(false).build(); cluster.waitActive(); fs = (cluster.getFileSystem()); checkFile(fs, file, replication); fc = FileContext.getFileContext(cluster.getURI(0)); assertTrue(fc.getFileLinkStatus(symlink).isSymlink()); } finally { if (fs != null) fs.close(); cleanup(cluster); cluster = null; } }