java

Calculate centroid of 2D non crossing polygon

Calculate centroid of 2D non crossing polygon,To accommodate that points are correct using Gift wrapping algorithm(Finding Convex Hull) Test case import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.awt.Point; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Test; public class MathUtilTest { @Test public void computeCentroidWithHull() { Point p1 = new Point(1, 1); Point p2 = new …

Calculate centroid of 2D non crossing polygon Read More »

Find Convex hull of given points using Gift wrapping algorithm

Find Convex hull of given points using Gift wrapping algorithm This is implementation of Grift wrapping algorithm for finding convex hull.   private static final Integer ZERO = new Integer(0);     /** * Find Convex hull of given points * * @ref http://en.wikipedia.org/wiki/Gift_wrapping_algorithm * @param vertices * @return */ private static List<Point> findConvexHull(final List<Point> …

Find Convex hull of given points using Gift wrapping algorithm Read More »

Convert Leptonica PIX data to Java BufferedImage

This snippet allows us to convert Leptonica PIX data into Java BufferedImage, in my case the pix->data could be compressed using zlib so I am decompressing before recreating image. We are assuming here that this is bi-tonal image(1 bpp) Sample Usage BufferedImage image = LeptonicaUtil.convert(zlibData, width, height, 1); ImageIO.write(image, "png", new File("C:/temp/test.png")); BufferedImage image = …

Convert Leptonica PIX data to Java BufferedImage Read More »

Encrypting application configuration files – Java AES

There other day I needed to encrypt certain properties in our XML configuration files. But there were few challanges that needed to be addressed Use existing Java installation !!! How will Master Encryption Key be derives and stored !!! First restriction is very important as it limits us to what algorithmic and key sizes can …

Encrypting application configuration files – Java AES Read More »

java.lang.ClassNotFoundException: org.springframework.web.context.support.StandardServletEnvironment

While upgrading from Spring 3.0.5 to 3.1.1.RELEASE I started getting following exception. java.lang.ClassNotFoundException: org.springframework.web.context.support.StandardServletEnvironment at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.springframework.web.servlet.HttpServletBean.<init>(HttpServletBean.java:90) at org.springframework.web.servlet.FrameworkServlet.<init>(FrameworkServlet.java:211) at org.springframework.web.servlet.DispatcherServlet.<init>(DispatcherServlet.java:323) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308)java.lang.ClassNotFoundException: org.springframework.web.context.support.StandardServletEnvironment at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.springframework.web.servlet.HttpServletBean.<init>(HttpServletBean.java:90) at org.springframework.web.servlet.FrameworkServlet.<init>(FrameworkServlet.java:211) at org.springframework.web.servlet.DispatcherServlet.<init>(DispatcherServlet.java:323) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) …

java.lang.ClassNotFoundException: org.springframework.web.context.support.StandardServletEnvironment Read More »

PhantomSQL released

Finally I have released my PhantomSQL project. PhantomSQL is a domain specific language designed for mining content from static and dynamic sources, It closely resembles SQL with features borrowed from other popular dynamic languages. It can be run as a interpreter or ‘server’ mode, it comes with type 4 JDBC driver for ease of integration …

PhantomSQL released Read More »

Proper way to read InputStream to byte array

There are many ways to accomplish this but this one does not use any external dependencies like Apache commons. Two common pitfalls that I see are that people forget to flush the ByteArrayOutputStream and they call ‘baos.write(buffer)’ instead of ‘baos.write(buffer, 0, read)’ without actually clearing the buffer, which causes the last write to append previous …

Proper way to read InputStream to byte array Read More »

Regex to remove DOCTYPE prolog

While using HTML Tidy I needed to remove the DOCTYPE prolog to prevent ‘org.xml.sax.SAXParseException: Already seen doctype.’ exception. Regex is quite simple, only catch is that we need to make sure we include the \n\r in our selecton and make it not greedy. convertedData = convertedData.replaceAll("<!DOCTYPE((.|\n|\r)*?)\">", ""); convertedData = convertedData.replaceAll("<!DOCTYPE((.|\n|\r)*?)\">", ""); This will consume multiline …

Regex to remove DOCTYPE prolog Read More »