MOA 12.03
Real Time Analytics for Data Streams
SerializeUtils.java
Go to the documentation of this file.
00001 /*
00002  *    SerializeUtils.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.core;
00021 
00022 import java.io.BufferedInputStream;
00023 import java.io.BufferedOutputStream;
00024 import java.io.ByteArrayInputStream;
00025 import java.io.ByteArrayOutputStream;
00026 import java.io.File;
00027 import java.io.FileInputStream;
00028 import java.io.FileOutputStream;
00029 import java.io.IOException;
00030 import java.io.ObjectInputStream;
00031 import java.io.ObjectOutputStream;
00032 import java.io.OutputStream;
00033 import java.io.Serializable;
00034 import java.util.zip.GZIPInputStream;
00035 import java.util.zip.GZIPOutputStream;
00036 
00043 public class SerializeUtils {
00044 
00045     public static class ByteCountingOutputStream extends OutputStream {
00046 
00047         protected int numBytesWritten = 0;
00048 
00049         public int getNumBytesWritten() {
00050             return this.numBytesWritten;
00051         }
00052 
00053         @Override
00054         public void write(int b) throws IOException {
00055             this.numBytesWritten++;
00056         }
00057 
00058         @Override
00059         public void write(byte[] b, int off, int len) throws IOException {
00060             this.numBytesWritten += len;
00061         }
00062 
00063         @Override
00064         public void write(byte[] b) throws IOException {
00065             this.numBytesWritten += b.length;
00066         }
00067     }
00068 
00069     public static void writeToFile(File file, Serializable obj)
00070             throws IOException {
00071         ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(
00072                 new BufferedOutputStream(new FileOutputStream(file))));
00073         out.writeObject(obj);
00074         out.flush();
00075         out.close();
00076     }
00077 
00078     public static Object readFromFile(File file) throws IOException,
00079             ClassNotFoundException {
00080         ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(
00081                 new BufferedInputStream(new FileInputStream(file))));
00082         Object obj = in.readObject();
00083         in.close();
00084         return obj;
00085     }
00086 
00087     public static Object copyObject(Serializable obj) throws Exception {
00088         ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
00089         ObjectOutputStream out = new ObjectOutputStream(
00090                 new BufferedOutputStream(baoStream));
00091         out.writeObject(obj);
00092         out.flush();
00093         out.close();
00094         byte[] byteArray = baoStream.toByteArray();
00095         ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
00096                 new ByteArrayInputStream(byteArray)));
00097         Object copy = in.readObject();
00098         in.close();
00099         return copy;
00100     }
00101 
00102     public static int measureObjectByteSize(Serializable obj) throws Exception {
00103         ByteCountingOutputStream bcoStream = new ByteCountingOutputStream();
00104         ObjectOutputStream out = new ObjectOutputStream(
00105                 new BufferedOutputStream(bcoStream));
00106         out.writeObject(obj);
00107         out.flush();
00108         out.close();
00109         return bcoStream.getNumBytesWritten();
00110     }
00111 }
 All Classes Namespaces Files Functions Variables Enumerations