iperf bandwidth testing woes – killing cleanly etc.

When running iperf on Windows platforms it is difficult to shut it down cleanly, without leaving stray child processes – especially if using within Java using ProcessBuilder. The solution I’ve discovered takes two approaches. Firstly run iperf server using the lesser known -P 1 switch, this causes it to shutdown after the client has connected.… Continue reading iperf bandwidth testing woes – killing cleanly etc.

Published
Categorized as geek Tagged ,

Unmarshalling JAXB entities from SOAP wrappers

Extracting JAXB entities from inside SOAP wrappers can be done without string-chopping using standard APIs. Example: using standard API. String example = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body><ns2:farm xmlns:ns2=\"http://adamish.com/example/farm\"><horse height=\"123\" name=\"glue factory\"/></ns2:farm></soapenv:Body></soapenv:Envelope>"; SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(example.getBytes())); Unmarshaller unmarshaller = JAXBContext.newInstance(Farm.class).createUnmarshaller(); Farm farm = (Farm)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument()); Anti-example seen in the wild: string chopping / regex String example =… Continue reading Unmarshalling JAXB entities from SOAP wrappers

Marshalling JAXB entities into SOAP wrappers

SOAP envelopes can be added to existing org.w3c.dom.Document or JAXB instances easily. Farm farm = new Farm(); farm.getHorse().add(new Horse()); farm.getHorse().get(0).setName("glue factory"); farm.getHorse().get(0).setHeight(BigInteger.valueOf(123)); Example: using standard API Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Marshaller marshaller = JAXBContext.newInstance(Farm.class).createMarshaller(); marshaller.marshal(farm, document); SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); soapMessage.getSOAPBody().addDocument(document); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); soapMessage.writeTo(outputStream); String output = new String(outputStream.toByteArray()); Anti-example seen in… Continue reading Marshalling JAXB entities into SOAP wrappers

Published
Categorized as geek Tagged ,

JNI helloworld

JNI is easy to use when you know how and frustrating when you don’t. I hope to demystify it and provide some pointers to when it goes wrong. Define your native methods using the native modifier. In this example we’ll add two numbers together. public class HelloWorld { public native int add(int a, int b);… Continue reading JNI helloworld

Published
Categorized as geek Tagged ,

SQLite with Hibernate/JPA LIMIT not working [solved]

Couldn’t get setMaxResults() / setFirstResult() to work properly in SQLite with Hibernate / JPA2. These ultimately boil down to SQL LIMIT/OFFSET. After much debug I found there is a problem in the widely distributed SQLiteDialect.java source. The following needed adding, checkout the method name, code smell! @Override public boolean bindLimitParametersInReverseOrder() { return true; }

Comparing maps

Using a combination of cloning constructors and removeAll() for subsetting… Map<String, String> beforeMap = new HashMap<String, String>(); beforeMap.put("a", "1"); beforeMap.put("b", "2"); beforeMap.put("c", "3"); Map<String, String> afterMap = new HashMap<String, String>(); afterMap.put("a", "1"); afterMap.put("c", "333"); Code Set<String> removedKeys = new HashSet<String>(beforeMap.keySet()); removedKeys.removeAll(afterMap.keySet()); Set<String> addedKeys = new HashSet<String>(afterMap.keySet()); addedKeys.removeAll(beforeMap.keySet()); Set<Entry<String, String>> changedEntries = new HashSet<Entry<String, String>>(afterMap.entrySet()); changedEntries.removeAll(beforeMap.entrySet());… Continue reading Comparing maps

Published
Categorized as geek Tagged

List partitioning

I’ve seen this done so many times in different ways, the easiest is definitely using subList. Note how use of Math.min removes need for a if statement. int partitionSize = 2; List<List<String>> partitions = new LinkedList<List<String>>(); for (int i = 0; i < messages.size(); i += partitionSize) { partitions.add(messages.subList(i, i + Math.min(partitionSize, messages.size() – i)));… Continue reading List partitioning

Published
Categorized as geek Tagged

Convert dotted IP to decimal IP – my comedy answer

String input = “134.115.64.1”; String sum = input.replaceAll(“(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)”, “($1 * 16777216) + ($2 * 65536) + ($3 * 256) + $4”); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); double result = (Double) engine.eval(sum); System.out.println((long) result);

Published
Categorized as geek Tagged ,

Chaining builders in Java

JavaFX uses chaining builders as a kind of syntactic sugar. Here’s an example. The first create() creates the builder (and inner text object), the subsequent calls set attributes on the the object, and finally it is returned via .build(). final Text text = TextBuilder.create() .text(“hello”) .x(50).y(100) .fill(Color.RED) .build(); Advantages. Slightly less code. Safer (as self-documenting)… Continue reading Chaining builders in Java

Published
Categorized as geek Tagged