Skip to content

Instantly share code, notes, and snippets.

package util;
import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by daniel on 06/07/2015.
*/
@danielshaya
danielshaya / LoopBackPingPong.java
Last active August 29, 2015 14:24
LoopBackPingPong.java
package util;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
package util;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import java.text.MessageFormat;
import java.util.*;
public class ADConnect {
package util;
import java.util.Random;
import java.util.stream.IntStream;
public class RandomString {
public static void main(String[] args) {
Random random = new Random();
IntStream.range(0,10).
forEach(i->System.out.println(generateRandomString(random, 9)));
public static void main(String[] args) {
try {
//Write with compression
//COMPILES BUT SO UGLY
writeToFile(new File("test"), "Hello World", i -> {
try {
return new GZIPOutputStream(i);
} catch (IOException e) {
//HOW ARE WE SUPPOSED TO DEAL WITH THIS EXCEPTION??
throw new AssertionError(e);
public static void main(String[] args) {
try {
//Write with compression
//DOES NOT COMPILE!!
writeToFile(new File("test"), "Hello World", GZIPOutputStream::new);
//Just use the FileOutputStream
writeToFile(new File("test"), "Hello World", i->i);
}catch(IOException e){
//deal with exception as you choose
}
private static void writeToFile(File file, String value,
Function<OutputStream, OutputStream> writing) throws IOException{
try (PrintWriter pw = new PrintWriter(new BufferedOutputStream
(writing.apply(new FileOutputStream(file))))) {
pw.write(value);
}
}
package util;
import java.io.*;
import java.util.zip.GZIPOutputStream;
public class LambdaExceptions {
public static void main(String[] args) {
try {
//Write with compression
writeToFile(new File("test"), "Hello World", GZIPOutputStream::new);
package util;
public class Allocation2 {
static int iterations = 10_000_000;
public static void main(String[] args) {
for (int i = 0; i <3 ; i++) {
testNoAllocations();
testAllocations();
package util;
public class Allocation1 {
static int iterations = 10_000_000;
public static void main(String[] args) {
for (int i = 0; i <3 ; i++) {
testNoAllocations();
testAllocations();
}