Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:com.sec.ose.osi.sdk.protexsdk.component.ComponentAPIWrapper.java

public static void save() {
    ObjectOutputStream oos;
    try {/*from   w  ww.  j  av  a2 s  .  c o m*/
        oos = new ObjectOutputStream(new FileOutputStream(new File(COMPONENT_FILE_PATH)));
        for (ComponentInfo tmp : globalComponentManager.getComponentList()) {
            oos.writeObject(tmp);
        }
        oos.flush();
        oos.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:com.beetle.framework.util.ObjectUtil.java

/**
 * /*from  ww  w . java  2 s  . c  o m*/
 * 
 * @param originObj
 * @return
 */
public final static Object objectClone(Object originObj) {
    ByteArrayOutputStream bao = null;
    ByteArrayInputStream bai = null;
    ObjectOutputStream oos;
    ObjectInputStream ois;
    try {
        bao = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bao);
        oos.writeObject(originObj);
        oos.flush();
        oos.close();
        bai = new ByteArrayInputStream(bao.toByteArray());
        ois = new ObjectInputStream(bai);
        Object obj = ois.readObject();
        ois.close();
        oos = null;
        ois = null;
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (bao != null) {
                bao.close();
                bao = null;
            }
            if (bai != null) {
                bai.close();
                bai = null;
            }
        } catch (IOException e) {
        }
    }
}

From source file:com.taobao.itemcenter.demo.utils.ItemUtils.java

public static byte[] getImageData() {
     byte[] imageData = null;
     try {//from   w w  w .  j ava  2  s .  co  m
         Object object = "test";
         ByteArrayOutputStream bo = new ByteArrayOutputStream();
         ObjectOutputStream oo = new ObjectOutputStream(bo);
         oo.writeObject(object);
         imageData = bo.toByteArray();
     } catch (Exception e) {
         return null;
     }
     return imageData;
 }

From source file:finale.year.stage.utility.Util.java

public static void emptyFile() {
    //Empty the File after User Has Logged Out
    //userFile.delete();

    String response = null;/*from   w  w  w  .  j  av a 2 s. c o  m*/
    ObjectOutputStream writer = null;
    userFile = new File("config.txt");

    //Write to File
    try {

        writer = new ObjectOutputStream(new FileOutputStream(userFile));
        writer.writeObject(""); //Write Empty to File
        writer.writeObject(""); //Write Empty to File
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();

    } catch (IOException ex) {
        ex.printStackTrace();

    } finally {
        if (writer != null) {
            try {
                writer.close(); //Close the Reader
            } catch (IOException ex) {
                ex.printStackTrace();
                return;
            }
        }
    } //End of If Block

}

From source file:finale.year.stage.utility.Util.java

/**
 * User wishes to save credentials on current system directory
 * First time instance , provided he supplies his credentials
 * Only checking for one user at present
 * @param email/* w  w  w.  j  a va2  s . c om*/
 * @param password 
 */
public static void rememberLogin(String email, String password) {
    //If user is has checked Remember me for first time
    String response = null;
    ObjectOutputStream writer = null;
    userFile = new File("config.txt");

    //Write to File
    try {

        writer = new ObjectOutputStream(new FileOutputStream(userFile));
        writer.writeObject(email); //Write Email to File
        writer.writeObject(encryptPassword(password)); //Write Password to File
        Authentification.handleResponse(login(email, encryptPassword(password)));
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();

    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close(); //Close the Reader
            } catch (IOException ex) {
                ex.printStackTrace();
                return;
            }
        }
    } //End of If Block
}

From source file:com.taobao.itemcenter.demo.utils.ItemUtils.java

public static List<ItemImageDO> getNullUrlImageList() {
     List<ItemImageDO> itemImageList = new ArrayList<ItemImageDO>();
     ItemImageDO itemImage = new ItemImageDO();
     itemImage.setImageName("imageName");
     itemImage.setImageUrl(null);// www .  ja va 2s.co m
     itemImage.setImagePosition(1);
     Object object = "test";
     byte[] imageData = null;
     try {
         ByteArrayOutputStream bo = new ByteArrayOutputStream();
         ObjectOutputStream oo = new ObjectOutputStream(bo);
         oo.writeObject(object);
         imageData = bo.toByteArray();
     } catch (Exception e) {
         return null;
     }

     itemImage.setImageData(imageData);
     itemImage.setProperties("sjdjfj");
     itemImage.setInputFileName("inputfile.jpg");
     itemImageList.add(itemImage);
     return itemImageList;
 }

From source file:cascading.util.Util.java

public static String serializeBase64(Object object, boolean compress) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    ObjectOutputStream out = new ObjectOutputStream(compress ? new GZIPOutputStream(bytes) : bytes);

    try {//from w ww .  j  a  v a  2s. c o m
        out.writeObject(object);
    } finally {
        out.close();
    }

    return new String(Base64.encodeBase64(bytes.toByteArray()));
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Returns a <code>byte[]</code> containing the byte representation
 * of the serializable object <code>obj</code>.
 * // w  w w . j a va 2 s .  com
 * @param obj the <code>Object</code> to convert to a byte array
 * @return <code>byte[]</code> containing the byte representation
 * of <code>obj</code>.
 * 
 * @throws IOException if <code>obj</code> is not serializable or error occurs while writing the bytes
 */
public static byte[] getObjectBytes(Object obj) throws IOException {
    ByteArrayOutputStream bout = null;
    ObjectOutputStream out = null;
    byte[] data = null;
    try {
        bout = new ByteArrayOutputStream(READ_BUFFER_SIZE);
        out = new ObjectOutputStream(bout);
        out.writeObject(obj);
        out.flush();

        data = bout.toByteArray();
    } finally {
        MiscUtils.closeStream(out);
    } //end finally

    return data;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.mallet.util.MalletUtils.java

public static void runTrainCRF(File trainingFile, File modelFile, double var, int iterations,
        String defaultLabel, boolean fullyConnected, int[] orders, boolean denseFeatureValues)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    Reader trainingFileReader = null;
    InstanceList trainingData = null;/*from w ww . j a  v a2 s. c o  m*/
    //trainingFileReader = new FileReader(trainingFile);
    trainingFileReader = new InputStreamReader(new GZIPInputStream(new FileInputStream(trainingFile)), "UTF-8");
    Pipe p = null;
    CRF crf = null;
    p = new ConversionToFeatureVectorSequence(denseFeatureValues); //uses first line of file to identify DKProInstanceID feature and discard
    p.getTargetAlphabet().lookupIndex(defaultLabel);
    p.setTargetProcessing(true);
    trainingData = new InstanceList(p);
    trainingData.addThruPipe(new LineGroupIterator(trainingFileReader, Pattern.compile("^\\s*$"), true)); //if you want to skip the line containing feature names, add "|^[A-Za-z]+.*$"
    //      logger.info
    //      ("Number of features in training data: "+p.getDataAlphabet().size());

    //      logger.info ("Number of predicates: "+p.getDataAlphabet().size());

    if (p.isTargetProcessing()) {
        Alphabet targets = p.getTargetAlphabet();
        StringBuffer buf = new StringBuffer("Labels:");
        for (int i = 0; i < targets.size(); i++) {
            buf.append(" ").append(targets.lookupObject(i).toString());
            //         logger.info(buf.toString());
        }
    }

    crf = trainCRF(trainingData, crf, var, iterations, defaultLabel, fullyConnected, orders);

    ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(modelFile));
    s.writeObject(crf);
    s.close();
}

From source file:com.jk.util.JKObjectUtil.java

/**
 * Copy.//from   www.j a  v  a 2 s. com
 *
 * @param source
 *            the source
 * @return the object
 */
// /////////////////////////////////////////////////////////////////
public static Object copy(Object source) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(source);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object deepCopy = ois.readObject();
        return deepCopy;
    } catch (Exception e) {
        throw new JKException(e);
    }
}