Archive

Archive for May, 2014

Jens wins CodeMint’s coding challenge

CodeMint’s coding challenge is over and Jens Lideström is the winner! Check out the results in order to learn about high-performance lambda functions and streams for Java 8. The text is in Swedish, but the code speaks for itself.

The challenge was to process weather data from NCDC as fast as possible using the new streams and lambda functions available in Java SE 8. Jens used a custom spliterator in order to win. My solution is short and to the point, but it is four seconds slower. Congratulations to Jens (who joins CodeMint as a new consultant Tomorrow!).

Categories: Java

First Google Glass application released

Erik-Google-Glass-small
Last week I released my first real GlassWare! It is a really cool application, but unfortunately under non-disclosure. Anyway, my first impressions of Google Glass as a target platform are fairly positive.
The API is still changing and that can make life a bit difficult. Some samples target the wrong version and either won’t compile or crash. Furthermore my computer wouldn’t recognize Google Glass correctly at first, so adb couldn’t find it as a device. I solved that as explained in this post. Finally the voice recognition for my Swedish accent is – well, let’s settle for not perfect.
All in all, though, it is fun to develop for Google Glass! The bleading edge is where I want to be.

Categories: Android, Google Glass, Java

Lazily read lines from gzip file with Java 8 streams

Java 8 can read lines lazily from files using the Files.lines method, but what to do if the files are compressed? I needed to do just that, so here is a class that provides a line stream from a gzipped file. It can easily be converted for other file formats!


public class GZIPFiles {
  /**
   * Get a lazily loaded stream of lines from a gzipped file, similar to
   * {@link Files#lines(java.nio.file.Path)}.
   * 
   * @param path
   *          The path to the gzipped file.
   * @return stream with lines.
   */
  public static Stream<String> lines(Path path) {
    InputStream fileIs = null;
    BufferedInputStream bufferedIs = null;
    GZIPInputStream gzipIs = null;
    try {
      fileIs = Files.newInputStream(path);
      // Even though GZIPInputStream has a buffer it reads individual bytes
      // when processing the header, better add a buffer in-between
      bufferedIs = new BufferedInputStream(fileIs, 65535);
      gzipIs = new GZIPInputStream(bufferedIs);
    } catch (IOException e) {
      closeSafely(gzipIs);
      closeSafely(bufferedIs);
      closeSafely(fileIs);
      throw new UncheckedIOException(e);
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(gzipIs));
    return reader.lines().onClose(() -> closeSafely(reader));
  }

  private static void closeSafely(Closeable closeable) {
    if (closeable != null) {
      try {
        closeable.close();
      } catch (IOException e) {
        // Ignore
      }
    }
  }
}

Share and enjoy…

Categories: Java