Example usage for java.lang Byte SIZE

List of usage examples for java.lang Byte SIZE

Introduction

In this page you can find the example usage for java.lang Byte SIZE.

Prototype

int SIZE

To view the source code for java.lang Byte SIZE.

Click Source Link

Document

The number of bits used to represent a byte value in two's complement binary form.

Usage

From source file:org.nd4j.linalg.util.ArrayUtil.java

/**
 *
 * @param byteArray/*from w  w w  .  j a  v a2 s.c  o m*/
 * @return
 */
public static float[] toFloatArray(byte[] byteArray) {
    int times = Float.SIZE / Byte.SIZE;
    float[] doubles = new float[byteArray.length / times];
    for (int i = 0; i < doubles.length; i++) {
        doubles[i] = ByteBuffer.wrap(byteArray, i * times, times).getFloat();
    }
    return doubles;
}

From source file:org.nd4j.linalg.util.ArrayUtil.java

/**
 *
 * @param intArray//from w w  w . jav a2  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/*  ww  w .j  a va2s . com*/
 * @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 {//  ww  w  . j  a va  2  s  . c  o  m
            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:au.org.ala.delta.util.Utils.java

/**
 * Use the values of the bits in the supplied array of bytes to create a
 * single array of boolean values/*  w  w  w  .  ja v a  2 s  . co  m*/
 */
public static boolean[] byteArrayToBooleanArray(byte[] bArray) {
    boolean[] boolArray = new boolean[bArray.length * Byte.SIZE];

    for (int i = 0; i < bArray.length; i++) {
        byte b = bArray[i];
        for (int j = 0; j < Byte.SIZE; j++) {
            if ((b & (1 << j)) > 0) {
                boolArray[i * Byte.SIZE + j] = true;
            } else {
                boolArray[i * Byte.SIZE + j] = false;
            }
        }
    }

    return boolArray;
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestCheckpoint.java

/**
 * Tests save namespace./*from   ww  w  .  j  a v a  2  s  .c om*/
 */
@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;
    }
}

From source file:au.org.ala.delta.intkey.model.IntkeyDatasetFileReader.java

/**
 * Read attributes from the items file/* w  w  w. j  av  a  2  s  . c  o  m*/
 * 
 * @param itemFileHeader
 *            item file header
 * @param itemBinFile
 *            item file data
 * @param c
 *            character that we want attributes for
 * @param taxa
 *            taxa that we want attributes for
 * @return a list of attributes for the supplied character and taxa.
 */
private static List<Attribute> readAttributes(ItemsFileHeader itemFileHeader, BinFile itemBinFile, Character c,
        List<Item> taxa) {
    List<Attribute> retList = new ArrayList<Attribute>();

    int totalNumChars = itemFileHeader.getNChar();
    int totalNumTaxa = itemFileHeader.getNItem();

    seekToRecord(itemBinFile, itemFileHeader.getRpCdat());
    List<Integer> charAttributeDataRecordIndicies = readIntegerList(itemBinFile, totalNumChars);

    // Subtract 1 from the charNo because characters are zero indexed in
    // intkey API
    int charNo = c.getCharacterId();
    int charTaxonDataRecordIndex = charAttributeDataRecordIndicies.get(charNo - 1);

    seekToRecord(itemBinFile, charTaxonDataRecordIndex);

    if (c instanceof MultiStateCharacter) {

        MultiStateCharacter multiStateChar = (MultiStateCharacter) c;

        int bitsPerTaxon = multiStateChar.getStates().length + 1;
        int totalBitsNeeded = bitsPerTaxon * totalNumTaxa;
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalBitsNeeded) / Double.valueOf(Byte.SIZE)))
                .intValue();

        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaData = Utils.byteArrayToBooleanArray(bytes);

        for (Item t : taxa) {
            int startIndex = (t.getItemNumber() - 1) * bitsPerTaxon; // Taxa
                                                                     // numbers
                                                                     // are
                                                                     // 1
                                                                     // indexed
                                                                     // instead
                                                                     // of 0
                                                                     // indexed
            int endIndex = startIndex + bitsPerTaxon;

            boolean[] taxonData = Arrays.copyOfRange(taxaData, startIndex, endIndex);

            // Taxon data consists of a bit for each state, indicating
            // the states presence, followed by
            // a final bit signifying whether or not the character is
            // inapplicable for the taxon.
            boolean inapplicable = taxonData[taxonData.length - 1];

            HashSet<Integer> presentStates = new HashSet<Integer>();
            for (int k = 0; k < taxonData.length - 1; k++) {
                boolean statePresent = taxonData[k];
                if (statePresent) {
                    presentStates.add(k + 1);
                }
            }

            SimpleAttributeData attrData = new SimpleAttributeData(presentStates.isEmpty(), inapplicable);
            MultiStateAttribute msAttr = new MultiStateAttribute(multiStateChar, attrData);
            msAttr.setItem(t);

            msAttr.setPresentStates(presentStates);

            retList.add(msAttr);
        }

    } else if (c instanceof IntegerCharacter) {
        IntegerCharacter intChar = (IntegerCharacter) c;
        int charMinValue = intChar.getMinimumValue();
        int charMaxValue = intChar.getMaximumValue();

        // 1 bit for all values below minimum, 1 bit for each value between
        // minimum and maximum (inclusive),
        // 1 bit for all values above maximum, 1 inapplicability bit.
        int bitsPerTaxon = charMaxValue - charMinValue + 4;
        int totalBitsNeeded = bitsPerTaxon * totalNumTaxa;

        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalBitsNeeded) / Double.valueOf(Byte.SIZE)))
                .intValue();

        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaData = Utils.byteArrayToBooleanArray(bytes);

        for (Item t : taxa) {
            int startIndex = (t.getItemNumber() - 1) * bitsPerTaxon; // Taxa
                                                                     // numbers
                                                                     // are
                                                                     // 1
                                                                     // indexed
                                                                     // instead
                                                                     // of 0
                                                                     // indexed
            int endIndex = startIndex + bitsPerTaxon;

            boolean[] taxonData = Arrays.copyOfRange(taxaData, startIndex, endIndex);

            boolean inapplicable = taxonData[taxonData.length - 1];

            Set<Integer> presentValues = new HashSet<Integer>();
            for (int k = 0; k < taxonData.length - 1; k++) {
                boolean present = taxonData[k];
                if (present) {
                    presentValues.add(k + charMinValue - 1);
                }
            }

            IntegerAttribute intAttr = new IntegerAttribute(intChar,
                    new SimpleAttributeData(presentValues.isEmpty(), inapplicable));
            intAttr.setItem(t);
            intAttr.setPresentValues(presentValues);

            retList.add(intAttr);
        }

    } else if (c instanceof RealCharacter) {
        // Read NI inapplicability bits
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalNumTaxa) / Double.valueOf(Byte.SIZE)))
                .intValue();
        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaInapplicabilityData = Utils.byteArrayToBooleanArray(bytes);

        int recordsSpannedByInapplicabilityData = recordsSpannedByBytes(bytesToRead);

        seekToRecord(itemBinFile, charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData);

        // Read two float values per taxon
        List<Float> taxonData = readFloatList(itemBinFile, totalNumTaxa * 2);

        for (Item t : taxa) {
            int taxonNumber = t.getItemNumber();

            float lowerFloat = taxonData.get((taxonNumber - 1) * 2);
            float upperFloat = taxonData.get(((taxonNumber - 1) * 2) + 1);

            boolean inapplicable = taxaInapplicabilityData[taxonNumber - 1];

            // Character is unknown for the corresponding taxon if
            // lowerfloat > upperfloat
            boolean unknown = lowerFloat > upperFloat;

            RealAttribute realAttr = new RealAttribute((RealCharacter) c,
                    new SimpleAttributeData(unknown, inapplicable));

            if (!unknown) {
                FloatRange range = new FloatRange(lowerFloat, upperFloat);
                realAttr.setPresentRange(range);
            }
            realAttr.setItem(t);

            retList.add(realAttr);
        }

    } else if (c instanceof TextCharacter) {
        TextCharacter textChar = (TextCharacter) c;

        // Read NI inapplicability bits
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalNumTaxa) / Double.valueOf(Byte.SIZE)))
                .intValue();
        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaInapplicabilityData = Utils.byteArrayToBooleanArray(bytes);

        int recordsSpannedByInapplicabilityData = recordsSpannedByBytes(bytesToRead);

        seekToRecord(itemBinFile, charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData);

        List<Integer> taxonTextDataOffsets = readIntegerList(itemBinFile, totalNumTaxa + 1);

        int recordsSpannedByOffsets = recordsSpannedByBytes((totalNumTaxa + 1) * Constants.SIZE_INT_IN_BYTES);

        seekToRecord(itemBinFile,
                charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData + recordsSpannedByOffsets);

        ByteBuffer taxonTextData = itemBinFile.readByteBuffer(
                taxonTextDataOffsets.get(taxonTextDataOffsets.size() - taxonTextDataOffsets.get(0)));

        for (Item t : taxa) {
            int taxonNumber = t.getItemNumber();

            int lowerOffset = taxonTextDataOffsets.get(taxonNumber - 1);
            int upperOffset = taxonTextDataOffsets.get((taxonNumber - 1) + 1);
            int textLength = upperOffset - lowerOffset;

            String txt = "";
            if (textLength > 0) {
                byte[] textBytes = new byte[textLength];
                taxonTextData.position(lowerOffset - 1);
                taxonTextData.get(textBytes);

                txt = BinFileEncoding.decode(textBytes);
            }

            boolean inapplicable = taxaInapplicabilityData[taxonNumber - 1];
            boolean unknown = StringUtils.isEmpty(txt);

            TextAttribute txtAttr = new TextAttribute(textChar, new SimpleAttributeData(unknown, inapplicable));
            try {
                txtAttr.setText(txt);
            } catch (DirectiveException e) {
                // The SimpleAttributeData implementation won't throw this
                // Exception.
            }
            txtAttr.setItem(t);

            retList.add(txtAttr);

        }
    }

    return retList;
}