Example usage for java.io ObjectInputStream readInt

List of usage examples for java.io ObjectInputStream readInt

Introduction

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

Prototype

public int readInt() throws IOException 

Source Link

Document

Reads a 32 bit int.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    int i = 123456;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeInt(i);//from w  w  w  .  j av  a 2 s  .c  o  m
    oout.writeInt(54321);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an int
    System.out.println(ois.readInt());

    // read and print an int
    System.out.println(ois.readInt());
    ois.close();
}

From source file:TestCipher.java

public static void main(String args[]) throws Exception {
    Set set = new HashSet();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Point point = new Point(random.nextInt(1000), random.nextInt(2000));
        set.add(point);/*from   w ww .j  a v a2s  . c  o  m*/
    }
    int last = random.nextInt(5000);

    // Create Key
    byte key[] = password.getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    // Create Cipher
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Create stream
    FileOutputStream fos = new FileOutputStream("out.des");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    ObjectOutputStream oos = new ObjectOutputStream(cos);

    // Write objects
    oos.writeObject(set);
    oos.writeInt(last);
    oos.flush();
    oos.close();

    // Change cipher mode
    desCipher.init(Cipher.DECRYPT_MODE, secretKey);

    // Create stream
    FileInputStream fis = new FileInputStream("out.des");
    BufferedInputStream bis = new BufferedInputStream(fis);
    CipherInputStream cis = new CipherInputStream(bis, desCipher);
    ObjectInputStream ois = new ObjectInputStream(cis);

    // Read objects
    Set set2 = (Set) ois.readObject();
    int last2 = ois.readInt();
    ois.close();

    // Compare original with what was read back
    int count = 0;
    if (set.equals(set2)) {
        System.out.println("Set1: " + set);
        System.out.println("Set2: " + set2);
        System.out.println("Sets are okay.");
        count++;
    }
    if (last == last2) {
        System.out.println("int1: " + last);
        System.out.println("int2: " + last2);
        System.out.println("ints are okay.");
        count++;
    }
    if (count != 2) {
        System.out.println("Problem during encryption/decryption");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));

    out.writeObject(Calendar.getInstance());
    out.writeObject(new BigDecimal("123.123"));
    out.writeInt(1);/*from  w  w  w.  j a  v a  2 s  .co  m*/
    out.writeUTF("tutorial");
    out.close();

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    BigDecimal price;
    int unit;
    String desc;

    Calendar date = (Calendar) in.readObject();
    System.out.println(date);

    price = (BigDecimal) in.readObject();
    unit = in.readInt();
    desc = in.readUTF();
    System.out.println(unit);
    System.out.println(desc);
    System.out.println(price);
    in.close();
}

From source file:DataIOTest2.java

public static void main(String[] args) throws IOException {

    // write the data out
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("invoice1.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);//from ww  w .  j  av a 2 s  .  c  o m
        out.writeChar('\t');
        out.writeInt(units[i]);
        out.writeChar('\t');
        out.writeChars(descs[i]);
        out.writeChar('\n');
    }
    out.close();

    // read it in again
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("invoice1.txt"));

    double price;
    int unit;
    String desc;
    double total = 0.0;

    try {
        while (true) {
            price = in.readDouble();
            in.readChar(); // throws out the tab
            unit = in.readInt();
            in.readChar(); // throws out the tab
            desc = in.readLine();
            System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
            total = total + unit * price;
        }
    } catch (EOFException e) {
    }
    System.out.println("For a TOTAL of: $" + total);
    in.close();
}

From source file:ObjectStreams.java

public static void main(String[] args) throws IOException, ClassNotFoundException {

    ObjectOutputStream out = null;
    try {//from   w  w  w  . j  a  va2  s  . c o  m
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

        out.writeObject(Calendar.getInstance());
        for (int i = 0; i < prices.length; i++) {
            out.writeObject(prices[i]);
            out.writeInt(units[i]);
            out.writeUTF(descs[i]);
        }
    } finally {
        out.close();
    }

    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

        Calendar date = null;
        BigDecimal price;
        int unit;
        String desc;
        BigDecimal total = new BigDecimal(0);

        date = (Calendar) in.readObject();

        System.out.format("On %tA, %<tB %<te, %<tY:%n", date);

        try {
            while (true) {
                price = (BigDecimal) in.readObject();
                unit = in.readInt();
                desc = in.readUTF();
                System.out.format("You ordered %d units of %s at $%.2f%n", unit, desc, price);
                total = total.add(price.multiply(new BigDecimal(unit)));
            }
        } catch (EOFException e) {
        }
        System.out.format("For a TOTAL of: $%.2f%n", total);
    } finally {
        in.close();
    }
}

From source file:Shape.java

public static void deserializeStaticState(ObjectInputStream os) throws IOException {
    color = os.readInt();
}

From source file:Main.java

public static void RestoreSharedPrefencesFromStream(ObjectInputStream input, Editor editor)
        throws IOException, ClassNotFoundException {
    int size = input.readInt();
    for (int i = 0; i < size; i++) {
        String key = input.readUTF();
        Object val = input.readObject();
        if (val instanceof Boolean)
            editor.putBoolean(key, ((Boolean) val).booleanValue());
        else if (val instanceof Float)
            editor.putFloat(key, ((Float) val).floatValue());
        else if (val instanceof Integer)
            editor.putInt(key, ((Integer) val).intValue());
        else if (val instanceof Long)
            editor.putLong(key, ((Long) val).longValue());
        else if (val instanceof String)
            editor.putString(key, ((String) val));
    }/*from   w  ww  . j  ava  2  s  . c  om*/
    editor.commit();
}

From source file:org.mrgeo.utils.Base64Utils.java

public static double[] decodeToDoubleArray(String encoded) throws IOException {
    byte[] objBytes = Base64.decodeBase64(encoded.getBytes());

    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;

    try {//from  w  w  w .  j  a  va2  s . c  o  m
        bais = new ByteArrayInputStream(objBytes);
        ois = new ObjectInputStream(bais);
        int arrayLength = ois.readInt();
        double result[] = new double[arrayLength];
        for (int i = 0; i < arrayLength; i++) {
            result[i] = ois.readDouble();
        }
        return result;
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}

From source file:de.tud.cs.se.flashcards.persistence.Store.java

public static FlashcardSeries openSeries(File file) throws IOException {

    ObjectInputStream oin = null;
    try {/* w w w  .  j  av  a 2 s  . co m*/

        FlashcardSeries series = new FlashcardSeries();

        oin = new ObjectInputStream(new FileInputStream(file));
        int size = oin.readInt();
        for (int i = 0; i < size; i++) {
            series.addCard((Flashcard) oin.readObject());
        }

        return series;
    } catch (ClassNotFoundException e) {
        // the file did contain something unexpected...
        throw new IOException(e);
    } finally {
        if (oin != null)
            IOUtils.closeQuietly(oin);
    }
}

From source file:tvbrowser.ui.filter.dlgs.FilterTreeModel.java

public static FilterTreeModel initInstance(ObjectInputStream in) throws IOException, ClassNotFoundException {
    int version = in.readInt();
    FilterNode rootNode = new FilterNode(in, version);
    fixRootNode(rootNode);//from  w  w w.j a  va 2s  .  c o m
    mInstance = new FilterTreeModel(rootNode);
    return mInstance;
}