home / blog

Tag Archives: java

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 … Continue reading

Posted in geek | Tagged , | Leave a comment

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 … Continue reading

Posted in geek | Tagged , , | 2 Comments

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(); … Continue reading

Posted in geek | Tagged , | Leave a comment

Cannot add tomcat7 server in eclipse 4.x [solved]

rm workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jst.server.tomcat.core.prefs rm workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs # restart eclipse

Posted in geek | Tagged , , , | Leave a comment

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 … Continue reading

Posted in geek | Tagged , | Leave a comment

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, … Continue reading

Posted in geek | Tagged , , , | Leave a comment

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()); … Continue reading

Posted in geek | Tagged | Leave a comment

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 = … Continue reading

Posted in geek | Tagged | Leave a comment

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);

Posted in geek | Tagged , | Leave a comment

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 … Continue reading

Posted in geek | Tagged | Leave a comment