Example usage for java.io DataInputStream readInt

List of usage examples for java.io DataInputStream readInt

Introduction

In this page you can find the example usage for java.io DataInputStream readInt.

Prototype

public final int readInt() throws IOException 

Source Link

Document

See the general contract of the readInt method of DataInput.

Usage

From source file:J2MESortMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*  w w w  . jav  a2  s  .  c  o m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "Mary", "Bob", "Adam" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();

            String[] inputString = new String[3];
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            StringBuffer buffer = new StringBuffer();
            comparator = new Comparator();
            recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                inputDataStream.reset();
            }
            alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
            inputDataStream.close();
            inputStream.close();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
                comparator.compareClose();
                recordEnumeration.destroy();
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }
}

From source file:netinf.node.resolution.bocaching.impl.BOCacheImpl.java

@Override
public boolean cache(DataObject dataObject) {
    String hash = DatamodelUtils.getHash(dataObject);
    String directory = server.getDirectory(); // .replace('\\', '/')

    if (!directory.endsWith(File.separator)) {
        directory += File.separator;
    }/*from   ww w .  ja v  a  2 s.c  om*/
    if (hash == null) {
        LOG.info("DataObject has no Hash and will not be cached");
        return false;
    }

    if (!contains(dataObject)) {
        LOG.log(DemoLevel.DEMO, "(BOCache ) Cache file...");
        List<Attribute> locators = dataObject
                .getAttributesForPurpose(DefinedAttributePurpose.LOCATOR_ATTRIBUTE.toString());
        for (Attribute attr : locators) {
            DataInputStream fis = null;
            String url = attr.getValue(String.class);
            try {
                String destination = directory + hash + ".tmp";
                transferDispatcher.getStreamAndSave(url, destination, true);

                // start reading
                fis = new DataInputStream(new FileInputStream(destination));

                // skip manually added content-type
                int skipSize = fis.readInt();
                for (int i = 0; i < skipSize; i++) {
                    fis.read();
                }

                byte[] hashBytes = Hashing.hashSHA1(fis);
                IOUtils.closeQuietly(fis);
                if (hash.equalsIgnoreCase(Utils.hexStringFromBytes(hashBytes))) {
                    LOG.info("Hash of downloaded file is valid: " + url);
                    LOG.log(DemoLevel.DEMO, "(NODE ) Hash of downloaded file is valid. Will be cached.");
                    File old = new File(destination);
                    File newFile = new File(destination.substring(0, destination.lastIndexOf('.')));
                    old.renameTo(newFile);
                    addLocator(dataObject);
                    cached.add(newFile.getName());

                    return true;
                } else {
                    LOG.log(DemoLevel.DEMO, "(NODE ) Hash of downloaded file is invalid. Trying next locator");
                    LOG.warn("Hash of downloaded file is not valid: " + url);
                    LOG.warn("Trying next locator");
                }

            } catch (FileNotFoundException ex) {
                LOG.warn("Error downloading:" + url);
            } catch (IOException e) {
                LOG.warn("Error hashing:" + url);
            } catch (Exception e) {
                LOG.warn("Error hashing, but file was OK: " + url);
                // e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(fis);
                rebuildCache();
            }
        }
        LOG.warn("Could not find reliable source to cache: " + dataObject);
        return false;
    } else {
        LOG.log(DemoLevel.DEMO, "(NODE ) DataObject has already been cached. Adding locator.");
        addLocator(dataObject);
        return true;
    }
}

From source file:org.ozsoft.xantippe.filestore.FileStore.java

/**
 * Reads the index file./* ww w. j  a  va  2 s  . co  m*/
 * 
 * @throws  IOException  if the file could not be read
 */
private void readIndexFile() throws IOException {
    entries.clear();
    File file = new File(dataDir, INDEX_FILE);
    if (file.exists()) {
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        int noOfEntries = dis.readInt();
        for (int i = 0; i < noOfEntries; i++) {
            int id = dis.readInt();
            int offset = dis.readInt();
            int length = dis.readInt();
            FileEntry entry = new FileEntry(id);
            entry.setOffset(offset);
            entry.setLength(length);
            entries.put(id, entry);
        }
        dis.close();
    }
}

From source file:SearchMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//from   w  w  w  . ja  va  2  s .  c o m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString[] = { "Adam", "Bob", "Mary" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            String inputString;
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            if (recordstore.getNumRecords() > 0) {
                filter = new Filter("Mary");
                recordEnumeration = recordstore.enumerateRecords(filter, null, false);
                while (recordEnumeration.hasNextElement()) {
                    recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                    inputString = inputDataStream.readUTF() + " " + inputDataStream.readInt();
                    alert = new Alert("Reading", inputString, null, AlertType.WARNING);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert);
                }
            }
            inputStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
                filter.filterClose();
                recordEnumeration.destroy();
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:com.facebook.infrastructure.db.Column.java

/**
 * Here we need to get the column and apply the filter.
 *//*  ww  w  . ja  va 2  s.c  o m*/
public IColumn deserialize(DataInputStream dis, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;

    String name = dis.readUTF();
    IColumn column = new Column(name);
    column = filter.filter(column, dis);
    if (column != null) {
        column = defreeze(dis, name);
    } else {
        /* Skip a boolean and the timestamp */
        dis.skip(DBConstants.boolSize_ + DBConstants.tsSize_);
        int size = dis.readInt();
        dis.skip(size);
    }
    return column;
}

From source file:ClipboardExample.java

public Object nativeToJava(TransferData transferData) {
    if (isSupportedType(transferData)) {

        byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null)
            return null;

        MyType[] myData = new MyType[0];
        try {//from  ww  w  .  jav  a 2 s  . co  m
            ByteArrayInputStream in = new ByteArrayInputStream(buffer);
            DataInputStream readIn = new DataInputStream(in);
            while (readIn.available() > 20) {
                MyType datum = new MyType();
                int size = readIn.readInt();
                byte[] name = new byte[size];
                readIn.read(name);
                datum.firstName = new String(name);
                size = readIn.readInt();
                name = new byte[size];
                readIn.read(name);
                datum.lastName = new String(name);
                MyType[] newMyData = new MyType[myData.length + 1];
                System.arraycopy(myData, 0, newMyData, 0, myData.length);
                newMyData[myData.length] = datum;
                myData = newMyData;
            }
            readIn.close();
        } catch (IOException ex) {
            return null;
        }
        return myData;
    }

    return null;
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPageMetaInfo.java

/**
 * Reads the static html parts from a file stored in a separate file in the same package as the precompiled GSP class
 *
 * @throws IOException//  ww w . j a va 2s .  c  om
 */
private void readHtmlData() throws IOException {
    String dataResourceName = resolveDataResourceName(HTML_DATA_POSTFIX);

    DataInputStream input = null;
    try {
        InputStream resourceStream = pageClass.getResourceAsStream(dataResourceName);

        if (resourceStream != null) {

            input = new DataInputStream(resourceStream);
            int arrayLen = input.readInt();
            htmlParts = new String[arrayLen];
            for (int i = 0; i < arrayLen; i++) {
                htmlParts[i] = input.readUTF();
            }
        }
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:com.momock.http.HttpSession.java

void readHeaders() {
    try {/*  w w  w .  j av  a  2s .  c o  m*/
        if (!fileInfo.exists() || fileInfo.length() == 0)
            return;
        DataInputStream din = new DataInputStream(new FileInputStream(fileInfo));
        headers = new TreeMap<String, List<String>>();
        int headerCount = din.readInt();
        for (int i = 0; i < headerCount; i++) {
            String key = din.readUTF();
            int count = din.readInt();
            List<String> vals = new ArrayList<String>();
            for (int j = 0; j < count; j++) {
                String val = din.readUTF();
                vals.add(val);
            }
            headers.put(key, vals);
        }
        din.close();
    } catch (IOException e) {
        Logger.error(e);
    }
}

From source file:MixedRecordEnumerationExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//ww  w  .  j  a v a  2s .c o m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString[] = { "First Record", "Second Record", "Third Record" };
            int outputInteger[] = { 15, 10, 5 };
            boolean outputBoolean[] = { true, false, true };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeBoolean(outputBoolean[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
            }
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            StringBuffer buffer = new StringBuffer();
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            recordEnumeration = recordstore.enumerateRecords(null, null, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append("\n");
                buffer.append(inputDataStream.readBoolean());
                buffer.append("\n");
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
            inputStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
                recordEnumeration.destroy();
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:org.prorefactor.refactor.PUB.java

/** Read the version, return false if the PUB file is out of date, true otherwise. */
private boolean readVersion(DataInputStream in) throws IOException {
    if (in.readInt() != LAYOUT_VERSION)
        return false;
    return true;/*  ww  w .ja  v a2s. c o m*/
}