Stream trouble

Monday, June 29, 2015 11:44:03 PM

I just had a stream of objects I wanted to sort and convert to a list. Optionally it should also be limited to a maximum number of entries. Piece of cake with Java 8:

Stream<T> stream = collection.stream().sorted(comparator);
if (max >= 0) {
  stream.limit(max);
}
List<T> result = stream.collect(Collectors.toList());

Or so I thought... The code above throws an IllegalStateException at runtime, stating that the “stream has already been operated upon or closed”.

The cause should be obvious. However it took me a while to find it, so I am posting it in case other people (possibly you when you came here via search engine) get stuck at the same place. Stream operations are very likely to return a different Stream object. The limit() method is such an example. In my code above, limit() operates on the stream and returns a limited stream. However I just throw away the returned stream and invoke collect() on the original stream, which was now already operated upon.

The solution is simple:

if (max >= 0) {
  stream = stream.limit(max);
}

Written by Shred in Javano comments

Tags: Java 8, Streams

Setting up TP-Link TL-SG108E with Linux

Tuesday, December 30, 2014 12:57:48 AM

Frankly, I didn’t expect it and I was somewhat disappointed when I found out that the TP-Link TL-SG108E Easy Smart Switch (and its little brother TL-SG105E) cannot be configured via web browser. And I was even more disappointed when I found out that, even though Linux and MacOS were listed on the retail box, the configuration tool Easy Smart Configuration Utility runs on Windows only. And they mean it! When started in a Windows VM, the utility does not see any switches.

So the devices are rather cheap for a smart switch, but they still come with a price: no web interface.

However, thanks to some help in the interwebs, I was finally able to run the Configuration Utility on Fedora Linux. It’s not that easy, though.

Written by Shred in Linux6 comments

Tags: Java, JavaFX, Network, TP-Link

Mavenize your Sass

Saturday, September 27, 2014 4:56:03 PM

Sass is a kind of precompiler for CSS that offers additional features like nested styles, variables or includes. The syntax of scss files is similar to standard CSS, so it’s very easy to learn.

My goal is to:

  • nicely integrate Sass into the Maven build process,
  • use Jetty to see the web application with the compiled css files, and
  • see changes to a Sass file live (without running Maven or restarting Jetty).

Written by Shred in Javano comments

Tags: CSS, Jetty, Sass, maven

Hibernate 4 schema generation with Maven

Sunday, August 31, 2014 11:23:44 PM

While upgrading my blog software Cilla to Java 8 and Hibernate 4, I found out that the old hibernate3-maven-plugin refused to create schema.sql files. Well, it wasn’t really surprising. The name of the plugin already implied that the plugin won’t play with the next major release of Hibernate.

I could not spot an official update of the plugin. Instead, I found Kai Moritz new Hibernate 4 maven plugin, which turned out to be very useful.

One key feature is to set up and initialize a local database for unit testing. I don’t need this feature for Cilla (yet ;-)). All I need is a hbm2ddl style generation of a SQL schema file for setting up new instances of my blog software from scratch. It turned out that the plugin was easily configured that way, and so it got almost a drop-in replacement for the old plugin.

This is what the section of the project’s pom file looks like:

<plugin>
    <groupId>de.juplo</groupId>
    <artifactId>hibernate4-maven-plugin</artifactId>
    <version>1.0.4</version>
    <executions>
        <execution>
            <goals>
                <goal>export</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <hibernateDialect>org.hibernate.dialect.PostgreSQL82Dialect</hibernateDialect>
        <target>NONE</target>
        <type>CREATE</type>
    </configuration>
</plugin>

With the target set to NONE, the schema.sql file is quietly generated while building the project. If set to SCRIPT, a copy will be dumped to stdout.

A CREATE type only generates create statements of the database. The default is BOTH, which creates drop and create statements.

Since no actual database is created, there is no need to add user, password and url parameters.

A list of all configuration options can be found here. The plugin is available at Maven Central.

Written by Shred in Javano comments

Tags: Hibernate, Maven

Little Java Regex Cookbook

Saturday, August 30, 2014 12:23:47 PM

Regular expressions, or short “Regex”, are a pattern of characters and metacharacters that can be used for matching strings. For example, the pattern “gr[ae]y” matches both the strings “gray” and “grey”.

While regular expressions are an integral part of other popular languages, they have been introduced to the Java world quite late in 2002, with the release of Java 1.4. Perl, certainly the mother language of modern regexes, already turned 15 that year.

Regexes are sometimes hard to understand, but once you got the hang of them, they will soon become your weapon of choice when you have to deal with texts.

In this article, I focus on Java code patterns for common scenarios. If you have never heard of regular expressions before, the Wikipedia article and the Pattern JavaDoc are good starting points. The Regex Crossword site is a great place to practice your regex skills.

F20: SANE und trotzdem keinen Scanner?

Friday, January 31, 2014 12:02:43 AM

Falls sich mal jemand wundert, warum unter Fedora 20 der Scanner auf einmal nicht mehr gefunden wird:

sudo yum install sane-backends-drivers-scanners

löst das Problem. Das Paket wird aus irgendeinem Grund nicht automatisch mitinstalliert.

Written by Shred in Fedoradono comments

Tags: F20, SANE

How to feed DDMS with gpsbabel

Sunday, June 9, 2013 1:44:40 PM

The Android Device Monitor is not just an aid for debugging applications, but also allows to simulate GPS positions, so you won’t need to actually run around in the countryside for testing your GPS app. But where to get test data from?

I have recorded some of my hiking trips with my Garmin GPS 60, and saved them in Garmin’s proprietary gdb file format. These files contain waypoints, routes and also recorded tracks.

The Swiss Army Knife for GPS files, gpsbabel, comes in handy for converting a gdb file into the GPX file format that can be read by DDMS. This is the line I used for conversion:

gpsbabel -i gdb -f hike-track.gdb -o gpx,gpxver=1.1 -F hike-track.gpx

Note the gpxver=1.1 option, as DDMS is unable to read GPX 1.0 files.

After converting and loading the GPX file into DDMS, I can now send single waypoints as GPS events to the emulated device. But beyond that, I can also play back a recorded track, and simulate that I carry around the emulated device on that track. This is very useful for testing GPS apps.

Written by Shred in Androidno comments

Tags: GPS, debugging

Validating the Android 4.2.2 RSA fingerprint

Sunday, May 26, 2013 3:45:17 PM

Android 4.2.2 comes with a new security feature. If you try to connect to your smartphone via adb and USB debugging, you will note that your device is marked as “offline”. Additionally, a dialog shows up on your device, presenting an RSA fingerprint of your computer and asking for confirmation to accept a connection.

The rationale is that if your device is lost or stolen, there is no way to read its content even if USB debugging was enabled.

Now, presenting an RSA fingerprint surely is a nice idea to avoid man-in-the-middle attacks. But how do you get that fingerprint in order to compare it with the one shown on the device? At first I thought there must be a command (or an adb option) that prints out the fingerprint, but I wasn’t able to locate one. After spending some time with my favourite search engine, I finally dug up a rather more than less complicated command line that prints out the footprint:

awk '{print $1}' < adbkey.pub | openssl base64 -A -d -a | openssl md5 -c | \
awk '{print $2}' | tr '[:lower:]' '[:upper:]'

The command must be executed in the directory where adb stores the adb key, which usually is ~/.android (or /root/.android if adb runs as root).

If you are somewhat security paranoid, you surely wonder why, on the one hand, Google shows a footprint on the device, but on the other hand makes it difficult to find out if that footprint actually belongs to your computer.

Written by Shred in Android1 comment

Tags: adb

maven-release-plugin and git fix

Thursday, May 2, 2013 11:57:34 PM

After hours of trying and wondering why my release scripts suddenly stopped working, I found out that maven-release-plugin seems to have an issue with git on recent systems. If you invoke mvn release:prepare and find out that the release process just runs against the current SNAPSHOT instead of the release version, you likely stumbled upon bug MRELEASE-812.

The reason for this issue seems to be that mvn release:prepare parses the output of git status. However the status is localized in recent versions of git, and maven-release-plugin fails to parse the localized output.

The coming fix will probably use git status --porcelain, which returns a machine-readable output. However, for the time being

LANG='en_US.UTF-8'
mvn release:prepare

is a valid workaround.

Written by Shred in Javano comments

Tags: git, maven

Fedora: SSD kurz und schmerzlos

Sunday, April 28, 2013 11:42:07 AM

Es gibt schon viele Artikel, wie man SSD-Festplatten richtig in Linux einbindet. Aber entweder sind sie unvollständig oder recht lange. Also, hier eine tl;dr-Fassung – SSD mit Fedora, kurz und schmerzlos.

Trimming

Wenn die SSD trimming kann (was mittlerweile bei allen SSDs auf dem Markt der Fall sein sollte), sollte es natürlich auch verwendet werden. Dadurch ermöglicht man wear levelling, gibt also der SSD die Möglichkeit, den Verschleiß der Speicherzellen zu verteilen.

In der /etc/fstab wird bei jedem Mountpoint, der auf eine SSD-Partition verweist, die Parameter discard angehängt. Eine gute Idee ist es außerdem, noatime hinzuzufügen, um die Schreibzugriffe auf die Platte zu reduzieren. Das sieht dann zum Beispiel so aus:

UUID=939446e3-9bb9-40a6-bf03-2d87bb8f5837 /                       ext4    discard,noatime        1 1
UUID=4f75261d-2e40-4e39-bf63-2a9c517fc73d /home                   ext4    discard,noatime        1 2
UUID=05db751b-5c2b-47da-8577-89ee30d90e56 swap                    swap    defaults        0 0

Das funktioniert mit reinen ext4-Partitionen, aber auch mit LVM- und RAID-Partitionen, jedoch nicht mit Partitionen, die mit LUKS verschlüsselt sind. Swap-Partitionen trimmen immer, ein discard-Parameter ist nicht erforderlich.

Nach einem Neustart sollte man einmalig alle SSD-Partitionen von Hand trimmen:

sudo fstrim -v /
sudo fstrim -v /home

I/O-Scheduler

Was bei mechanischen Festplatten wirklich Zeit kostet, ist das Positionieren des Schreib-Lesekopfes, weshalb Linux versucht, die Daten möglichst zu sammeln und zu gruppieren. Bei SSDs spielt es dagegen keine Rolle, wie fragmentiert die Daten sind. Aus dem Grund kann man das Gruppieren wegfallen lassen und sich über die gewonnene Performance freuen.

Dazu wird eine Datei /etc/udev/rules.d/40-ssd.rules mit folgendem Inhalt angelegt:

ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="noop"

Beim nächsten Neustart verwenden SSD-Platten den noop-Scheduler, mechanische Festplatten weiterhin den für sie optimalen cfq.

Swappiness

SSDs können beliebig oft und schnell gelesen werden, verschleißen aber bei Schreibzugriffen. Swapping auf eine SSD-Partition ist zwar möglich, aber der Lebensdauer nicht sehr zuträglich. Folgende Zeilen in der /etc/sysctl.conf reduzieren das Auslagern auf die Swap-Partition auf ein Minimum.

vm.swappiness=1
vm.vfs_cache_pressure=50

Bei den heutigen Preisen selbst für üppige RAM-Ausstattung wäre es zumindest bei Desktop-Rechnern eine Überlegung wert, ob eine Swap-Partition überhaupt notwendig ist. Nachträglich kann eine Swap-Partition durch Auskommentieren der entsprechenden Zeile in der /etc/fstab deaktiviert werden.

Firefox-Cache

Firefox lagert seinen Cache in das Home-Verzeichnis aus, was eine zusätzliche Belastung für die SSD darstellt. Wer einen Rechner sein Eigen nennt, der mit üppig viel RAM gesegnet ist, kann auf das /tmp-Verzeichnis ausweichen, welches bei Fedora 18 im Arbeitsspeicher statt auf der Festplatte liegt. Das geht leider nur über einen Eingriff in die Eingeweide des Browsers über die URL about:config.

Nach einer Bestätigung, dass man sich benehmen wird, wird mit der rechten Maustaste über Neu - String ein neuer String-Eintrag angelegt. Der Eigenschaftsname lautet browser.cache.disk.parent_directory, der String-Wert /tmp.

Danach muss der Firefox noch neu gestartet werden.

Written by Shred in Fedoradono comments

Tags: Pimp My Fedora, SSD