Skip to content

Instantly share code, notes, and snippets.

@chandu-io
Last active July 25, 2019 04:36
Show Gist options
  • Select an option

  • Save chandu-io/11011308 to your computer and use it in GitHub Desktop.

Select an option

Save chandu-io/11011308 to your computer and use it in GitHub Desktop.
java :: Serialization and Encryption
Serialization article
http://www.ibm.com/developerworks/library/j-5things1/
SignedObject and SealedObject example
http://www.javaworld.com/article/2076237/learn-java/signed-and-sealed-objects-deliver-secure-serialized-content.html
ObjectInputValidation example
http://www.jguru.com/faq/view.jsp?EID=7476
DES Encryption example
http://timarcher.com/blog/2007/04/simple-java-class-to-des-encrypt-strings-such-as-passwords-and-credit-card-numbers/
http://sanjaal.com/java/186/java-encryption/tutorial-java-des-encryption-and-decryption/
http://www.mkyong.com/java/jce-encryption-data-encryption-standard-des-tutorial/
// Awesome enum example
//http://stackoverflow.com/questions/116574/java-get-file-size-efficiently
import java.io.*;
import java.net.*;
import java.util.*;
public enum FileSizeBench {
LENGTH {
@Override
public long getResult() throws Exception {
File me = new File(FileSizeBench.class.getResource(
"FileSizeBench.class").getFile());
return me.length();
}
},
CHANNEL {
@Override
public long getResult() throws Exception {
FileInputStream fis = null;
try {
File me = new File(FileSizeBench.class.getResource(
"FileSizeBench.class").getFile());
fis = new FileInputStream(me);
return fis.getChannel().size();
} finally {
fis.close();
}
}
},
URL {
@Override
public long getResult() throws Exception {
InputStream stream = null;
try {
URL url = FileSizeBench.class
.getResource("FileSizeBench.class");
stream = url.openStream();
return stream.available();
} finally {
stream.close();
}
}
};
public abstract long getResult() throws Exception;
public static void main(String[] args) throws Exception {
int runs = 5;
int iterations = 50;
EnumMap<FileSizeBench, Long> durations = new EnumMap<FileSizeBench, Long>(FileSizeBench.class);
for (int i = 0; i < runs; i++) {
for (FileSizeBench test : values()) {
if (!durations.containsKey(test)) {
durations.put(test, 0l);
}
long duration = testNow(test, iterations);
durations.put(test, durations.get(test) + duration);
// System.out.println(test + " took: " + duration + ", per iteration: " + ((double)duration / (double)iterations));
}
}
for (Map.Entry<FileSizeBench, Long> entry : durations.entrySet()) {
System.out.println();
System.out.println(entry.getKey() + " sum: " + entry.getValue() + ", per Iteration: " + ((double)entry.getValue() / (double)(runs * iterations)));
}
}
private static long testNow(FileSizeBench test, int iterations)
throws Exception {
long result = -1;
long before = System.nanoTime();
for (int i = 0; i < iterations; i++) {
if (result == -1) {
result = test.getResult();
//System.out.println(result);
} else if ((result = test.getResult()) != result) {
throw new Exception("variance detected!");
}
}
return (System.nanoTime() - before) / 1000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment