Example usage for java.io ObjectInputStream readUTF

List of usage examples for java.io ObjectInputStream readUTF

Introduction

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

Prototype

public String readUTF() throws IOException 

Source Link

Document

Reads a String in modified UTF-8 format.

Usage

From source file:Main.java

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

    String s = "Hello World from java2s.com!";

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

    oout.writeUTF(s);/*from  www  .j a  va  2s  .  c o  m*/
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    System.out.println(ois.readUTF());

    System.out.println(ois.readUTF());
    ois.close();
}

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);//  w  ww.  j av  a  2s .c  o 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:ObjectStreams.java

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

    ObjectOutputStream out = null;
    try {//from  w  w  w .j a  va2 s .c  om
        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: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));
    }// ww  w. java 2  s.c  o m
    editor.commit();
}

From source file:nl.opengeogroep.filesetsync.client.SyncJobStatePersistence.java

static void initialize() {

    File f = new File(SyncConfig.getInstance().getVarDir() + File.separator + "syncjobstate.dat");
    if (!f.exists()) {
        instance = new SyncJobStatePersistence();
    } else {/*  w ww.ja va 2 s .  co  m*/
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);

            String version = ois.readUTF();

            if (!Version.getProjectVersion().equals(version)) {
                log.warn("Sync state saved with version " + version + ", ignoring");
            } else {
                instance = (SyncJobStatePersistence) ois.readObject();
                ois.close();
                instance.cleanup();
                log.debug("Read sync job state from " + f.getAbsolutePath() + ", filesets: "
                        + instance.states.keySet().toString());
                if (log.isTraceEnabled()) {
                    for (Map.Entry<String, SyncJobState> entry : instance.states.entrySet()) {
                        log.trace("Fileset " + entry.getKey() + ": " + entry.getValue().toString());
                    }
                }
            }
        } catch (Exception e) {
            log.error("Error reading sync job state from " + f.getAbsolutePath() + ", starting with new state",
                    e);
            instance = new SyncJobStatePersistence();
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }
}

From source file:org.apache.hyracks.control.nc.service.NCService.java

private static boolean acceptConnection(InputStream is) {
    // Simple on-wire protocol:
    // magic cookie (string)
    // either:/*from  ww w  .  ja  va  2s.  c  om*/
    //   START_NC, ini file
    // or:
    //   TERMINATE
    // If we see anything else or have any error, crap out and await a different connection.
    try {
        ObjectInputStream ois = new ObjectInputStream(is);
        String magic = ois.readUTF();
        if (!ServiceConstants.NC_SERVICE_MAGIC_COOKIE.equals(magic)) {
            LOGGER.severe("Connection used incorrect magic cookie");
            return false;
        }
        switch (ServiceCommand.valueOf(ois.readUTF())) {
        case START_NC:
            String iniString = ois.readUTF();
            ini = new Ini(new StringReader(iniString));
            ncId = IniUtils.getString(ini, "localnc", "id", "");
            nodeSection = "nc/" + ncId;
            return launchNCProcess();
        case TERMINATE:
            LOGGER.info("Terminating NCService based on command from CC");
            exit(0);
            break;
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error decoding connection from server", e);
    }
    return false;
}

From source file:org.jbpm.console.ng.documents.backend.server.marshalling.CMISDocumentMarshallingStrategy.java

@Override
public Object read(ObjectInputStream os) throws IOException, ClassNotFoundException {
    String objectId = os.readUTF();
    String canonicalName = os.readUTF();
    String link = os.readUTF();//from  w w  w. ja va  2 s  . com
    try {
        DocumentSummary doc = (DocumentSummary) this.documentService.getDocument(objectId);
        Document document = (Document) Class.forName(canonicalName).newInstance();
        document.setIdentifier(objectId);
        document.setLink(link);
        document.setName(doc.getName());
        document.setSize(10);
        document.setLastModified(new Date());
        document.setAttributes(new HashMap<String, String>());
        document.setContent(doc.getContent());
        return document;
    } catch (Exception e) {
        throw new RuntimeException("Cannot read document", e);
    }
}

From source file:org.apache.openejb.log.commonslogging.OpenEJBCommonsLog.java

private void readObject(final ObjectInputStream in) throws IOException {
    logger = Logger.getInstance(LogCategory.OPENEJB, in.readUTF());
}

From source file:edu.vt.middleware.webflow.ClientFlowExecutionKey.java

private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    final ClientFlowExecutionKey temp = parse(in.readUTF());
    this.id = temp.id;
    this.data = temp.data;
}

From source file:pt.webdetails.cpk.elements.impl.KettleResult.java

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();/*from  ww w .j  a  va  2s  .c o  m*/

    String resultXmlString = in.readUTF();
    try {
        Document document = XMLHandler.loadXMLString(resultXmlString);
        Node resultNode = XMLHandler.getSubNode(document, Result.XML_TAG);
        this.result = new Result(resultNode);
    } catch (KettleException e) {
        this.logger.error("Unable to deserialize KettleResult.", e);
    }
}