List of usage examples for java.io ObjectOutputStream ObjectOutputStream
public ObjectOutputStream(OutputStream out) throws IOException
From source file:PersonExt.java
public static void main(String[] args) { PersonExt p1 = new PersonExt("John", "Male", 6.7); PersonExt p2 = new PersonExt("Wally", "Male", 5.7); PersonExt p3 = new PersonExt("Katrina", "Female", 5.4); File fileObject = new File("personext.ser"); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileObject))) { oos.writeObject(p1);//from w w w . j av a 2 s. com oos.writeObject(p2); oos.writeObject(p3); System.out.println(p1); System.out.println(p2); System.out.println(p3); } catch (IOException e1) { e1.printStackTrace(); } fileObject = new File("personext.ser"); try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileObject))) { p1 = (PersonExt) ois.readObject(); p2 = (PersonExt) ois.readObject(); p3 = (PersonExt) ois.readObject(); // Let's display the objects that are read System.out.println(p1); System.out.println(p2); System.out.println(p3); // Print the input path System.out.println("Objects were read from " + fileObject.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.sf.jabb.util.text.test.KeywordMatcherExample.java
/** * @param args//from w w w .j a va 2 s .c o m * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("==== ===="); KeywordMatcher m = showExample(null); System.out.println("==== ? ===="); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(m); byte[] binary = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(binary); ObjectInputStream ois = new ObjectInputStream(bais); KeywordMatcher m2 = (KeywordMatcher) ois.readObject(); showExample(m2); System.out.println("==== ? ===="); KeywordMatcher m3 = new KeywordMatcher(m); showExample(m3); }
From source file:Blip1.java
public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); Blip1 b1 = new Blip1(); Blip2 b2 = new Blip2(); ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blips.out")); System.out.println("Saving objects:"); o.writeObject(b1);/*from w w w . j av a 2 s . co m*/ o.writeObject(b2); o.close(); // Now get them back: ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out")); System.out.println("Recovering b1:"); b1 = (Blip1) in.readObject(); // OOPS! Throws an exception: //! System.out.println("Recovering b2:"); //! b2 = (Blip2)in.readObject(); }
From source file:Blip3.java
public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); Blip3 b3 = new Blip3("A String ", 47); System.out.println(b3);/*ww w . ja v a 2s . co m*/ ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blip3.out")); System.out.println("Saving object:"); o.writeObject(b3); o.close(); // Now get it back: ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blip3.out")); System.out.println("Recovering b3:"); b3 = (Blip3) in.readObject(); System.out.println(b3); }
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 w w. jav a 2 s .com*/ } 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:ObjectStreamTest.java
public static void main(String[] args) { Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15); carl.setSecretary(harry);/* ww w. j a v a 2 s . c o m*/ Manager tony = new Manager("Tony Tester", 40000, 1990, 3, 15); tony.setSecretary(harry); Employee[] staff = new Employee[3]; staff[0] = carl; staff[1] = harry; staff[2] = tony; try { // save all employee records to the file employee.dat ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat")); out.writeObject(staff); out.close(); // retrieve all records into a new array ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat")); Employee[] newStaff = (Employee[]) in.readObject(); in.close(); // raise secretary's salary newStaff[1].raiseSalary(10); // print the newly read employee records for (Employee e : newStaff) System.out.println(e); } catch (Exception e) { e.printStackTrace(); } }
From source file:AESTest.java
public static void main(String[] args) { try {// w ww.j a v a 2 s . co m if (args[0].equals("-genkey")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(key); out.close(); } else { int mode; if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE; else mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key key = (Key) keyIn.readObject(); keyIn.close(); InputStream in = new FileInputStream(args[1]); OutputStream out = new FileOutputStream(args[2]); Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
From source file:SignatureTest.java
public static void main(String[] args) { try {//from w ww . j a v a 2 s. c o m if (args[0].equals("-genkeypair")) { KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = new SecureRandom(); pairgen.initialize(KEYSIZE, random); KeyPair keyPair = pairgen.generateKeyPair(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(keyPair.getPublic()); out.close(); out = new ObjectOutputStream(new FileOutputStream(args[2])); out.writeObject(keyPair.getPrivate()); out.close(); } else if (args[0].equals("-sign")) { ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); PrivateKey privkey = (PrivateKey) keyIn.readObject(); keyIn.close(); Signature signalg = Signature.getInstance("DSA"); signalg.initSign(privkey); File infile = new File(args[1]); InputStream in = new FileInputStream(infile); int length = (int) infile.length(); byte[] message = new byte[length]; in.read(message, 0, length); in.close(); signalg.update(message); byte[] signature = signalg.sign(); DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2])); int signlength = signature.length; out.writeInt(signlength); out.write(signature, 0, signlength); out.write(message, 0, length); out.close(); } else if (args[0].equals("-verify")) { ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2])); PublicKey pubkey = (PublicKey) keyIn.readObject(); keyIn.close(); Signature verifyalg = Signature.getInstance("DSA"); verifyalg.initVerify(pubkey); File infile = new File(args[1]); DataInputStream in = new DataInputStream(new FileInputStream(infile)); int signlength = in.readInt(); byte[] signature = new byte[signlength]; in.read(signature, 0, signlength); int length = (int) infile.length() - signlength - 4; byte[] message = new byte[length]; in.read(message, 0, length); in.close(); verifyalg.update(message); if (!verifyalg.verify(signature)) System.out.print("not "); System.out.println("verified"); } } catch (Exception e) { e.printStackTrace(); } }
From source file:cn.lynx.emi.license.GenerateLicense.java
public static void main(String[] args) throws ClassNotFoundException, ParseException { if (args == null || args.length != 4) { System.err.println("Please provide [machine code] [cpu] [memory in gb] [yyyy-MM-dd] as parameter"); return;/*from w w w.ja v a2s. c om*/ } InputStream is = GenerateLicense.class.getResourceAsStream("/privatekey"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String key = null; try { key = br.readLine(); } catch (IOException e) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); e.printStackTrace(); return; } if (key == null) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); return; } String machineCode = args[0]; int cpu = Integer.parseInt(args[1]); long mem = Long.parseLong(args[2]) * 1024 * 1024 * 1024; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); long expDate = sdf.parse(args[3]).getTime(); LicenseBean lb = new LicenseBean(); lb.setCpuCount(cpu); lb.setMemCount(mem); lb.setMachineCode(machineCode); lb.setExpireDate(expDate); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(baos); os.writeObject(lb); os.close(); String serializedLicense = Base64.encodeBase64String(baos.toByteArray()); System.out.println("License:" + encrypt(key, serializedLicense)); } catch (IOException e) { e.printStackTrace(); } }
From source file:RSATest.java
public static void main(String[] args) { try {//from www .j a v a 2 s . com if (args[0].equals("-genkey")) { KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); pairgen.initialize(KEYSIZE, random); KeyPair keyPair = pairgen.generateKeyPair(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(keyPair.getPublic()); out.close(); out = new ObjectOutputStream(new FileOutputStream(args[2])); out.writeObject(keyPair.getPrivate()); out.close(); } else if (args[0].equals("-encrypt")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); // wrap with RSA public key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key publicKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, publicKey); byte[] wrappedKey = cipher.wrap(key); DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2])); out.writeInt(wrappedKey.length); out.write(wrappedKey); InputStream in = new FileInputStream(args[1]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } else { DataInputStream in = new DataInputStream(new FileInputStream(args[1])); int length = in.readInt(); byte[] wrappedKey = new byte[length]; in.read(wrappedKey, 0, length); // unwrap with RSA private key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key privateKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.UNWRAP_MODE, privateKey); Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); OutputStream out = new FileOutputStream(args[2]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }