[jug.bg] Valhalla hackaton (29.01.2015)

Today we had a second bgjug hackaton for the month. Its topic was Valhalla.

Project Valhalla will probably be part of Java 10 (expected 2018). Among other things it allows using primitives as generic arguments.

Here's how you can build Valhalla yourself.

Here's a virtual machine with valhalla if you're lazy (virtual box).

This is the valhalla presentation by Ivan. And this is the video of the talk (in Bulgarian).


Here are some of the code we ran:

Ivan created a simple ArrayList

public class SpecialArrayList {
	private static int INITIAL_SIZE = 10;
	private T[] elements = null;
	private int length = 0;
	public SpecialArrayList() {
		elements = new T[INITIAL_SIZE];
	public T get(int position) {
		if(position >= length) {
			throw new ArrayIndexOutOfBoundsException();
		return elements[position];
	public void add(T element) {
		// Check whether array is full
		elements[length] = element;
public class Main {
	public static void main(String[] args) {
		SpecialArrayList intList = new SpecialArrayList<>();
		SpecialArrayList stringList = new SpecialArrayList<>();

Nayden tested a bit of crazy code - which is supposed to fail (IA impl = new IAImpl();):

public class B  {
   protected T t;
   protected T[] ts;
   public B(){}
   public B(T t){
     this.t = t;
     this.ts = new T[100];
   //  System.out.println(this.t.getClass());
   public T[] getTs(){
     return this.ts;
   public T getT(){
     return this.t;
   public static void main(String[] args){
       B b = new B<>(100);
       System.out.println((new B("dada")).getClass());
       IAImpl ludnica = new IAImpl();
       System.out.println(ludnica.getT() instanceof Object);
       IABase base = new IAChild();
       IA impl = new IAImpl();
interface IA{
 public T getT();
 public void setT(T t);
class IABase{
 private T t;
 public T getT(){
   return this.t;
 public void setT(T t){
   this.t = t;
class IAChild extends IABase {
class IAImpl implements IA {
 private Object t;
 public Object getT(){
   return this.t;
 public void setT(Object t){
   this.t = t;

Building OpenJDK 9 on a OSX (or any linux)

This is a tiny tutorial on how to build your own copy of OpenJDK 9 from the current sources.


├─Install a package system
├─Install ccache
└─Command Line Tools
Download OpenJDK 9
Build OpenJDK 9
├─JTREG: The variables
└─Running tests


Install a package system

Install brew or macports. I prefer brew. Both are package systems for OS X.

Install ccache

ccache is a compiler cache - speeds up the build.

$ brew install ccache

Command Line Tools

We need them so we have a C compiler and other tools.

Install Xcode Command Line tools ( you don't need the whole Xcode, only the command line tools)

Download OpenJDK 9

Get the sources, in the console type:

$ mkdir jkd9
$ cd jdk9
$ hg clone http://hg.openjdk.java.net/jdk9/dev jdk9.hg

Takes 10-20 secs. This downloads basically a couple of scripts that we will execute to get the actual sources.

Now we execute the script we downloaded:

$ cd jdk9.hg
$ chmod u+x get_source.sh
$ ./get_source.sh

The last command can can take 10-20 minutes depending on the connection speed.

Build OpenJDK 9

$ chmod u+x configure
$ ./configure
$ make clean images

The second command took a bit less than 20 minutes. The first command is fast.

The result: we have this created:  build/macosx-x86_64-normal-server-release/images/jdk/ - this is the built OpenJDK 9 - SUCCESS.


jtreg is a "testing harness" (whatever that means) made specially for OpenJDK. We need it to run the tests written for the OpenJDK

Download from https://adopt-openjdk.ci.cloudbees.com/view/OpenJDK/job/jtreg/ (not the Snapshot version, the other one).

unzip in jdk9, so we have jdk9/jtreg.

JTREG: The variables

The official instructions suggest we edit ~/.bashrc. I don't like changing global (for your user) settings, so we can put these settings in jdk9/.bashrc

$ nano .bashrc

Add the following lines:

export SOURCE_CODE=/Users/steve/jdk9/
export PRODUCT_HOME=$SOURCE_CODE/jdk9.hg/build/macosx-x86_64-normal-server-release/images/jdk
export PATH=$PATH:$PRODUCT_HOME/bin:$JT_HOME/linux/bin/

NOTE: Change /Users/steve/jdk9/ with your folder on the top line.
NOTE: Check the correctness of PRODUCT_HOME too.

To execute this file, do:

$ source .bashrc

Running tests

$ cd jdk9.hg/test
$ make jdk_util


Webrev crawls over your changes to generate a set of web-based views of the differences in your code. The different views allow reviewers to easily look at your proposed changes, selecting the appropriate difference format based on the type of file, the type of change, and their own preferences.

webrev.ksh is a 70kb kshell script. Pretty weird.

Go to jdk9.hg and:

jdk9.hg# wget http://hg.openjdk.java.net/code-tools/webrev/raw-file/tip/webrev.ksh
jdk9.hg# chmod u+x webrev.ksh

To prepare a webrev, make some changes. For example in the jaxp folder. Then we commit locally in the mercurial repo.

jdk9.hg# cd jaxp
jaxp# hg commit -m "BGJUG: Fix compiler warnings in jaxp repository"
jaxp# ../webrev.ksh

It will prepare a webrev/ dir and webrev.zip. The folder is then uploaded somewhere and sent to the appropriate mail list inside openjdk. Example:

Note: execution times come are measured on a pretty recent Macbook Air.

Source: https://java.net/projects/adoptopenjdk/pages/AdoptOpenJDKBuildInstructions

[jug.bg] Building Valhalla and using primitives as generic arguments

Project Valhalla will probably be part of Java 10 (expected 2018). Among other things it allows using primitives as generic arguments.

Building Valhalla

On a linux/unix/osx box we start with:

hg clone http://hg.openjdk.java.net/valhalla/valhalla
cd valhalla
bash get_source.sh  #this one takes some time
bash configure
make clean images # this one takes some cpu


We can have a class like this:

public class Program  {
	public static void main(String[] args) {
		// this one still doesn't work, List not updated to List
//		List l = new ArrayList();
//		l.add(5);
//		System.out.println(l.get(0));
		Box b = new Box(6);
class Box {
    private final T t;
    public Box(T t) { this.t = t; }
    public T get() { return t; }


Running javap gives

$ javap -c Program.class gives:
[openjdk@localhost bin]$ ./javap -c Program.class 
Compiled from "Program.java"
public class Program {
  public Program();
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return
  public static void main(java.lang.String[]);
       0: new           #2                  // class "Box${0=I}"
       3: dup
       4: bipush        6
       6: invokespecial #3                  // Method "Box${0=I}"."":(I)V
       9: astore_1
      10: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      13: aload_1
      14: invokevirtual #5                  // Method "Box${0=I}".get:()I
      17: invokevirtual #6                  // Method java/io/PrintStream.println:(I)V
      20: return

Running our pretty simple app gives:

[openjdk@localhost bin]$ ./java Program
Specializing Box${0=I}; searching for Box.class (not found)
Specializing Box${0=I}; searching for Box.class (found)
[openjdk@localhost bin]$

Notes: List is still not parameterized with because ... issues.

Building OpenJDK and submitting a solution on a feature request with the Bulgarian Java User Group

So yesterday was a memorable day.

We gathered the Bulgarian Java User Group and, as a hands-on-lab, in 3 hours we created a pretty complex patch on a open issue in the JDK Bug System. We plan to submit a webrev patch after we polish the code and supply better tests.

We've been playing with the OpenJDK code for quite some time. We have setup an easily distributable virtual machine (actually two virtual machines - fedora and ubuntu) that can be found here:
They have everything in there with clear instructions on how to build the code and start IntelliJ IDEA as the IDE.

The issue is this one:
That has been a request for quite some time. Apache Commons Lang even has an utility method.

Here are the slides of the hackaton:
And here are the instructions:

We even had some beer:

The patch included a pretty heated discussion on whether we should use the Apache's Commons Lang approach:

public static String getStackTrace(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    return sw.getBuffer().toString();

or we should rewrite some private classes inside java.lang.Throwable.

Here's the updated Throwable code:
First we added a method:

 * Returns string representation of this throwable and it's backtrace.
 * @return string representation of this {@code Throwable} and it's backtrace
public String getStackTraceString() {
    final StringBuilder sb = new StringBuilder();
    printStackTrace(new WrappedStringBuilder(sb));
    return sb.toString();

We renamed the ill-named PrintStreamOrWriter to StackTraceFacade and added another implementation:

private static class WrappedStringBuilder extends  StackTraceFacade {
    private static final String NEW_LINE = System.getProperty("line.separator");
    private final StringBuilder builder;
    WrappedStringBuilder(StringBuilder builder) {
        this.builder = builder;
    Object lock() {
        return builder;
    void println(Object o) {

We decided to reimplement and change the private inner class, because the Commons Lang uses StringWriter that internally uses the StringBuffer which is synchronized and we don't want that, since we sync on an upper level.
Here's a small JTREG test we created:

import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import java.lang.Throwable;
 * @test
 * @bug 5050783
 * @summary Basic test of serialization of stack trace information
 * @author Bulgarian JUG
 * @run testng java.lang.Throwable.GetStackTraceString
public class GetStackTraceString {
    public void doTest() {
            double a =100/0;
        }catch (Throwable t){

USB over IP - connect directly to a remote usb device

Problem1: My printer's scanner does not work over the wire with OSX. So the scanner part works by cable only. Well what if I put the usb cable over the wi-fi? Well, now I have a wireless scanner.

Problem2: I have a remote backup drive that I want to keep bootable and encrypted. Was it possible before this? I couldn't make it work. Is it possible now? I think so.

VirtualHere is a commercial software. For free it allows one simultaneous device, if you disconnect the first, you can use the second. It works flawlessly.

Its server has builds for most linuxes plus OpenWrt. The client is very simple, builds for osx, win, linux.

There are FOSS alternatives. If anyone has tried them, please share some info.

OSX: hide an app from the Command+Tab

Open the config file for the app

Last login: Tue Nov 25 21:29:02 on ttys001
ff:~ steve$ sudo nano /Applications/iTerm.app/Contents/Info.plist

Add the selected text:
Screen Shot 2014-11-25 at 9.29.59 PM

Text to add:

        <!-- mihail.stoynov: the following is to hide the iTerm.app from the finder -->


Last login: Tue Nov 25 21:29:02 on ttys001
ff:~ steve$ killall iTerm

OpenWRT: installing a bit-torrent with web interface

1 Install tranmission

opkg update
opkg install transmission-cli transmission-daemon transmission-web

2 configuration

don't use /etc/config/transmission - it is not read by the service starter. Actually, don't use /etc/init.d/transmission start - it doesn't work for some reason - I'm too lazy to use it.

What I did is:

transmission-daemon -d >> settings.json

which doesn't work also for some reason, so copy the config info and save to settings.json or edit /root/.config/transmission-daemon/settings.json directly

3. changes to settings.json

"download-dir": "/home/ftp_user/storage/",
"rpc-authentication-required": true,
"rpc-enabled": true,
"rpc-password": "pass",
"rpc-url": "/transmission/",
"rpc-username": "user",
"rpc-whitelist-enabled": false,

4 start

if you saved a local copy of settings.json

transmission-daemon -g .

if you changed /root/.config/transmissio-daemon/settings.json


OpenWRT: usb flash or usb disk as a storage device in OpenWrt

1. Find the plugged in USB device


to see plugged in devices.

2 Partitions

install and use cfdisk to partion the USB.

3. GPT

gdisk not implemented on OpenWRT, so if the disk is GPT, then create an empty partition and it can be formatted with mkfs.ext4.

Then to recognize which partition is which (recognize them by size), use

cat /proc/partitions

 4. ext4

Install e2fsprogs, so that you can use mkfs.ext4.

mkfs.ext4 /dev/sdb5

5. fstab

Edit /etc/config/fstab

config 'mount'
	option	target	'/mnt/sda1'
	option	device	'/home/ftp_user/8GB'
	option	enabled	'0'
config 'mount'
	option  target  '/home/ftp_user/storage'
	option  device  '/dev/sdb5'
	option  enable  '0'

OpenWRT: ftp installation and configuration (vsftpd)

1 open holes in the firewall

Use OpenWRT's web interface (luci): open tcp:20 and tcp:21 for plain old unencrypted ftp.

2 install vsftpd

opkg update
opkg install vsftpd

3 create the ftp directory

mkdir -p /home/ftp_user/storage
chown ftp_user /home/ftp_user/storage

Note: Don't chown the ftp_user dir, because later we will jail the user.

4 add the user to the system

Add a user by editing /etc/passwd, useradd doesn't add a shell, so don't use it.

"/bin/false" - no ssh login possible



passwd ftp_user

to change the password of that user

5 configure vsftpd

Config, add these to /etc/vsftpd.conf, so the anon user points to the right location


6 jail the user


7 start and enable

to start and make it autostart at boot:

/etc/init.d/vsftpd start
/etc/init.d/vsftpd enable

Basically OpenWRT has issues in terms of scripts and commands, but it's amazing how many things are already implemented.

Airport Utility protocol port: tcp:5009

Sometimes I need to access an Airport Extreme router (Apple's own wi-fi router) that's hidden behind a NAT.

There is a way to do that with Bonjour, but I haven't made it work.

I do that with local port forwarding via ssh, and the port is 5009 (add -L5009:, and then open The airport utility and click File->Configure Other... and then enter localhost as the address, and type your password.

Screen Shot 2014-11-22 at 3.14.45 PM

Finder: new text file?

AppleScript to the rescue again:

on run {input, parameters}
	tell application "Finder" to make new file at (the target of the front window) as alias
	return input
end run

Screen Shot 2014-11-17 at 6.46.00 PM


Save it as an Automator Application, then while holding the Command, drag to Finder toolbar to add as a button.

Macbooks: powerered usb ports?

So sometimes you want to charge something from the usb of a macbook while the macbook is closed (sleeping, or whatever this functionality is called nowadays). Other manufacturer like ASUS have one usb port that is always powered, while the rest are not.

This is what I found.
With macbooks that's not the case. If the macbook is closed, all the ports are unpowered.
If you start charging something while the macbook is open, and then close it, it will continue to charge it while closed. Which is a really cool, because initially I thought all the ports are unpowered.

This is a bit annoying, because if closed, you have to open the macbook first.

Tested on the latest MBA.

Yosemite: Finder to show folders before other files

sudo cp /System/library/CoreServices/Finder.app/Contents/Resources/English.lproj/InfoPlist.strings /System/library/CoreServices/Finder.app/Contents/Resources/English.lproj/InfoPlist.strings.bak
#convert from binary to xml format
sudo plutil -convert xml1 /System/library/CoreServices/Finder.app/Contents/Resources/English.lproj/InfoPlist.strings
sudo nano /System/library/CoreServices/Finder.app/Contents/Resources/English.lproj/InfoPlist.strings
# add a space so it looks like: <value> Folder</value>, check the screenshot
sudo plutil -convert binary1 /System/library/CoreServices/Finder.app/Contents/Resources/English.lproj/InfoPlist.strings
sudo killall Finder


qbit links to the wrong library

Qbit depends on openssl, but the one installed from macports.

If you see:
Dyld Error Message:
Library not loaded: /opt/local/lib/libcrypto.1.0.0.dylib
Referenced from: /Applications/qbittorrent.app/Contents/Frameworks/libtorrent-rasterbar.7.dylib
Reason: image not found

Fix it with:
sudo cp /Applications/qbittorrent.app/Contents/Frameworks/libcrypto.1.0.0.dylib /opt/local/lib/

or with:

sudo port install openssl

macports vs. homebrew

&gt;sudo port install imagemagick

Goes for 40 min, still not done, I got bored and stopped. And my laptop was overheating.

&gt;brew install imagemagick

Done in 15 seconds.

The reason is that macports compiles everything and the ports are very granularized, while with homebrew most items are less granularized and are precompiled.

Talk smtp to gmail with openssl s_client

Here are the basic commands to talk smtp to gmail.

We will send email from sender@gmail.com to recepient@gmail.com
The gmail password of sender@gmail.com is "my secret password".


To authenticate, we need our user/pass in base64 format:
base64("sender@gmail.com") = c2VuZGVyQGdtYWlsLmNvbQ0K
base64("my secret password") = bXkgc2VjcmV0IHBhc3N3b3Jk

To get the base64 encoded string, google "base64 online encoder" and click on any of the online encoder/decoders.

If you're using Gmail's two-step authentication

Go to https://security.google.com/settings/security/apppasswords and get a one-time password.

Ending the DATA of the email.

To end the DATA part, we need to press dot (".") and then Enter (which should send CRLF).

Important NOTE: I'm on a macbook, and the terminal client sends LF when I press enter. When I want to send CRLF, I press Ctrl+V, Enter. If you don't know what I'm talking about, after the dot (".") if it doesn't work with dot and Enter, press [dot, Ctrl+V, Enter].

The commands

We will use S_client which is like telnet, but supports SSL (encrypted telnet). You will need OpenSSL for that purpose.

[mihail@arch ~]# openssl s_client -connect smtp.gmail.com:587 -starttls smtp
[a lot of text will be printed - ssl info. For simplicity ignore it.]
auth login
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
235 2.7.0 Accepted
250 mx.google.com at your service
mail from:<sender@gmail.com>
250 2.1.0 OK dc8smxxxxwib.7 - gsmtp
rcpt to:<recepient@gmail.com>
250 2.1.5 OK dc8smxxxxwib.7 - gsmtp
354 Go ahead dc8smxxxxwib.7 - gsmtp
subject:manual smtp with gmail
some text as the body of the email
more lines of text

250 2.0.0 OK 1414600919 dc8smxxxxwib.7 - gsmtp
221 2.0.0 closing connection dc8smxxxxwib.7 - gsmtp
[mihail@arch ~]#




If you want to use SSL 465, the command is:
#openssl s_client -connect smtp.gmail.com:465 -tls1

(here you need to start with HELO, and then AUTH LOGIN - I don't know why)


Also, some accounts fail with:

3073894076:error:1409E0E5:SSL routines:SSL3_WRITE_BYTES:ssl handshake failure:s3_pkt.c:598:

I don't know why. It seems like it wants to fall back to ssl3.

FBI is on their way to your house (a south-african scheme to steal money)

I am selling my old motorcycle and it is listed on an online auctioning site. Following is my communication with a south-african guy trying to steal some money from me. I continued the communication with him as far as possible, because I was interested how these things go. My info is anonymised, his info is not, so that google may cache his name name and contact info. The fun part is in bold.

--- I receive a text from +1 (717) 826 0150:
Is ur bike for sales?if yes get back to me via email (dan.rowey147@hotmail.com)

--- I reply to the email:
My bike is for sale. You sent me a text. Wanna buy it?

--- him:
I am really interested in the immediate purchase of your merchandise and i will like to know how long have you owned it??  what's your best price for it and when last was it serviced?

--- me:
Now what?

--- him:
Thank you for getting back to me. Can you assure me that it's in good state and that i will not be disappointed with it.I'm ready to pay your asking price and to be honest, i wanted to buy this for my Son, but the issue is i am an oceanographer and i do have a contract to go for which starts tomorrow and am leaving any moment from now.The contract is strictly no call due to the lack of reception on the sea area. But I'm able to access email anytime as we will make use of laptop so my only quickest payment option is PayPal as i can send money via PayPal anytime.Since I'm requesting this transaction to be done via PayPal, i will be responsible for all the paypal charges on this transaction and if you don't have an account with paypal, its pretty easy, safe and secured to open one. Just log on to http://www.paypal.com. I hope we can make the purchase as fast as possible? I have a mover that will come for it once payment clears and they will take care of very necessary paper for me. So i look forward to hear from you soon. will like to see more pics.
I need the address where the agent will meet you and your PayPal Full name and email address so i can send the money now.

--- me: (newly created paypal account)
paypal account: xxxxxxxxx@gmail.com

--- him:
i have just completed the payment via PayPal. A total of €1,650.00 EUR was sent,€1,100.00 EUR for the bike and the extra €500.00 EUR for the shipping charges and €50.00 EUR for the western union charges am sure you notice that,which you will be sending the €500.00 EUR to the address below via western union.I will advice you to check your inbox or spam mail of junk am sure you will find the Paypal confirmation email in one of those box.

Name : D A V I D - O L A G U N J U
Address: 65 Celliers street,
City: Pretoria
Post Code: 0002
Country: South Africa.

The shipper would be coming around to your area to have the bike picked up once you have sent the shipping charges fee to them,as i need you to send me your home address for the pickup and let me know what time you want them to come for the pick up.

I will be waiting to hear from you once the money has been sent to the shipper.

--- him again:
i receive a message from paypal stating that a legal action will be take against you since you refuse play your role in the transaction going on between me and you and now i don't even know what to do because am confuse.So please lets try and sort this out try as much as possible to the money and settle the shipper because i wont like paypal taking a brutal action against you... Wish to read from you soon.

--- me:
Oh my god. What should i do to stop that? I have a lot of money in paypal and i don't want it blocked.

--- him:
Hi, it's up to you as well to act fast as I'd already made the full payment through PayPal. There is nothing I could do from my side to influence PayPal rules. It's better you act according to PayPal request or I mail PayPal to suspend the transaction.I look forward to hear from you soon...

--- me:
I already sent you the money. I am at work now and the receipt is at home. tomorrow I am going to scan it, because I don't have a scanner at home.

Please don't suspend the transaction.

--- him:
You can send the following details to me so that i can forward it to PayPal management..

(1) Sender's Name;
(2) Receiver's Name;
(3) MTCN (Money Transfer Control Number);
(4) Amount Sent;

Get back to me asap..

--- him:
Stephen De-Guerre
1650 EURO

You are ask to send the scan receipt to me,so that we can confirm the transaction.

--- me:
I can do that tomorrow evening.

--- him:
Send it now...........Cause FBI is on their way to your house.

--- me:

--- him:
Send it when you get home...

--- me:
PROMISE ME YOU WILL STOP THE FBI. I don't have a scanner at home. I can scan it at work only. Only if the scanner is free and nobody is in the office. I will wait after everyone leaves. So 5pm tomorrow

--- him:
I will ask PayPal management to stop the FBI....So get back to me with the scan receipt by 5pm tomorrow..

--- me:
Thank you, thank you, thank you, thank you. Why are you being so mean?

--- He stopped the communication ---

TRIM suppport in OS X Maverics

Check System Information on TRIM support, should say No

sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
sudo perl -pi -e 's|(^\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
sudo touch /System/Library/Extensions/


Check System Information on TRIM support, should say Yes

Source: http://www.return1.at/trim-enabler-for-osx/

Update: my writes went from ~333mb/sec to 400-475 mb/sec on blackmagic disk speed test

[BG] Вървят ли двама, таблатура и ноти за китара, акорди.

Това са ноти и таблатура за цялота песен.

Вариант 1: много подробно, всеки акорд е разложен отделно. За начинаещи.

pdf с ноти
pdf само таблатура.

pdf с нотиpdf само таблатура

Вариант 2: само акордите, един е разложен за пример, добавих и текста в пдф файла.


Screen Shot 2014-08-17 at 12.42.21 AM

Софтуер: TuxGuitar (free, java based, има го за Win, OSX, Linux). Ето и tx файловете, ако някой иска да редактира и/или поправя грешки.

Вървят ли двама, tuxguitar, таблатури и ноти


Execute xpath queries on html downloaded from a url with java using eclipse luna

FYI: Eclipse Luna (4.4) is currently beta.

Create new Maven Project.

Open the pom.xml

Add the following dependencies:


The code is as follows:

package sty.qainjava.xpath.on.html;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.Charset;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
import org.w3c.dom.Document;
 * QAinJava: how to do an xpath on html in java.
 * We use <a href="http://htmlcleaner.sourceforge.net/">HtmlCleaner</a>
 * and <a href="https://hc.apache.org/">HttpClient</a>.
 * @author Mihail STY
public class Program {
	 * We're not using any methods so that the source code is as straight
	 * forward as possible.
	 * No exception handling at all for simplicity
	public static void main(String[] args) throws IOException,
	        ParserConfigurationException, XPathExpressionException,
	        TransformerException {
		String address = "https://www.google.com/";
		String html;
			// the httpclient part
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(address);
			CloseableHttpResponse response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			ContentType contentType = ContentType.getOrDefault(entity);
			Charset charset = contentType.getCharset();
			BufferedReader r = new BufferedReader(new InputStreamReader(
			        entity.getContent(), charset));
			// we can directly plug the input to HtmlCleaner,
			// but we put it in a string so we can print it,
			// or save it to a file
			String line = null;
			StringBuilder builder = new StringBuilder();
			while ((line = r.readLine()) != null) {
			html = builder.toString();
		{// write html to a file
			BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(
			        new FileOutputStream("google.html.xml")));
			// exception handling is not exceptionally good, but that's not our
			// focus here
		// HtmlCleaner part
		TagNode tagNode = new HtmlCleaner().clean(html);
		String cleanHtml = new SimpleHtmlSerializer(new CleanerProperties())
		// System.out.println(cleanHtml);
		{// write cleanHtml to a file
			BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(
			        new FileOutputStream("clean.html.xml")));
			// exception handling is not exceptionally good, but that's not our
			// focus here
		// we need a DOM document to execute xpath, HtmlCleaner helps in creating one
		Document doc = new DomSerializer(new CleanerProperties())
		{// save dom to file with a transformer (just for testing purposes)
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();
			transformer.transform(new DOMSource(doc), new StreamResult(
			        new File("dom.html.xml")));
		// the xpath part
		XPath xpath = XPathFactory.newInstance().newXPath();
		String imgURL = (String) xpath.evaluate("//img/@src", doc,
		//using two URLs we can make sure we get the absolute URL even if relative.
		System.out.println(new URL(new URL(address), imgURL).toString());

TrueCrypt insecure?

There is a warning on the homepage of truecrypt


>WARNING: Using TrueCrypt is not secure as it may contain unfixed security issues

More info:



http://truecrypt.ch/ (forking or actually continuing the development)
https://ciphershed.org/ (truecrypt fork)

TrueCrypt's audit in pdf:

Two problems

  • Some people, when confronted with a problem, think, "I know, I'll use regular expressions." Now they have two problems.
  • Some people, when faced with a problem, think, "I know, I'll use #binary." Now they have 10 problems.
  • Some people, when confronted with a problem, think, "I know, I'll use #threads," and then two they hav erpoblesms.
  • Some people, when confronted with a problem, think "I know, I'll use #multithreading". Nothhw tpe yawrve o oblems.
  • Some people, when confronted with a problem, think, "I know, I'll use mutexes." Now they have
  • Some people, when confronted with a problem, think: "I know, I'll use caching." Now they have one problems.
  • Some people see a problem and think "I know, I'll use #Java!" Now they have a ProblemFactory.
  • Some programmers, when confronted with a problem, think "I know, I'll use floating point arithmetic." Now they have 1.999999999997 problems.
  • Some people, wanting an escape from their full-time job, think "I know, I'll contribute to open source." Now they have two full-time jobs.
  • Some people, when confronted with a problem, think: "I know, I'll think outside the box!" Now, they have 3.75 problems, an entirely new framework, and three dozen toll house cookies cooling in the kitchen.
  • Some people when confronted with a desire to use pithy quotes in their presentations think "I know, I'll use something from Star Wars". Now two problems they have.
  • Some people, when confronted with a problem, think, "I know, I'll use #UTF8." Now they à??????µ?ç°§ùÔ_¦Ñ?.
  • "I'll use #PHP!" Now they have ("1 apple" + "1 orange") problems.
  • "I'll use #Perl!" Now they have more than one way to have more than one problem....
  • Some people, when confronted with a problem, think, "I know, I'll use Shareware." Now they have two trials.
  • Some people, when confronted with a problem, think, "I know, I'll use delegations." Now their problem is a problem of their problem.
  • Some people when confronted with a problem think "I know, I'll quote jwz". Now everyone has a problem.

Source: http://nedbatchelder.com/blog/201204/two_problems.html

Wineskin vs. PlayOnMac vs. WineBottler vs. CrossOver for Microsoft Office on a Mac



Microsoft Office is a de-facto standard. The discontinued version for OS X has an appalling interface, is not very interoperable and lacks the Ribbon interface. The iWorks is not a good solution if most of the people you work with use .docx, and ppt and .xsl.

Office 2010 and 2013 are not well supported on wine (just now a preview with 2013 was shown on wine on linux). So Office 2007 is used.

My criteria: how well it works, how well it integrates, can it print directly, can it use the keyboard layout of the OS X.

All the softwares shown are wrappers of wine.

PlayOnMac (ver. 4.2.2) FREE

  • installation wasn't easy
  • when installing office, it downloads .net, fonts and stuff automatically
  • printing on an HP printer (first install the printer on the mac, then it appears AFAIK only if default drivers in windows exist) - prints, but crashes the app.
  • couldn't find a way to add new keyboard layouts
  • bad support
  • open with - no actual info, couldn't make it work
  • MS Word works pretty good
  • Excel took a while to open a 5mb file
  • Overall: works but not sufficient for me

WineBottler (ver. 1.7.11) FREE

  • straight forward interface.
  • presets available
  • most of the built-in installers (IE6, Opera, Firefox...) don't work. Some of them do.
  • Uninstaller on the Q&A page. Doesn't work. You can delete stuff manually.
  • Installing MS Office fails for some reason (Please insert volume 'OFFICE12' (needed for package 'office2007pro')). I don't have a solution:


  • Overall: I can't make it work


Wineskin Winery (ver. 1.7) FREE

  • installation is not very straight-forward
  • fails to install - most of the offices tested fail to initialize, one started  but didn't finish
  • no presets on microsoft office - probably that's why it doesn't work
  • Overall: can't make it work at all

CrossOver (ver. 13.0.1) $50

  • installation with presets
  • Has a preset for Microsoft Office 2010 that works.
  • Supports the keyboard layout of the host.
  • Sees printers installed with the host (I don't know if drivers are needed).
  • "Open with" works by default.
  • Overall: unfortunately the only one that works fine.

Simple and light bootable flash to change NT password (I tested on Windows Vista)

I recently had to fix a changed Administrator password on a Windows Vista (most probably by malware, it disabled all admin users and changed passwords).

One can only use the Ubuntu Desktop installer with chntpw, but it is large and it's pointless to use it for this task. I found a very simple distro to use for the same task.

There is one tool I would not recommend: Ophcrack. It is supposed to "hack" (actually an unoptimized bruteforcer). I couldn't find a way to just "change" the pass. It didn't have chntpw bundled.

Here's a link: http://pogostick.net/~pnh/ntpasswd/ (it is very small, AFAIR only 3-10mb).

Gallery3 patch

There's this php photo album - Gallery3. I needed it to share images but the outlook had to be different:

  • the title of the pics is HH:MM
  • Separator between pictures from different days.
  • Ordered by exif.date-picture-taken
  • Upload from a phone (only current option is via ftp and then load the pics to the album via a module).
  • Special simple comments section.

This is the patch - it's ugly and inconsistent, but it works for me. My php is not that awesome 🙂



(in gallery3 with modules)# patch -p1 < gallery3.diet.ver3.patch

Mapping the OSI model to the TCP/IP stack

I have always had issues with mapping OSI to TCP/IP. This is a nice picture:

OSI TCP Model Comparison

Left is OSI, right is TCP/IP.

Source: http://www.thebryantadvantage.com/OSI%20TCPIP%20Model%20Mapping%20CCNA%20Certification%20Tutorial.htm

My difficulty was mapping the Session from OSI in a http connection. Or is the http protocol a presentation or application layer. According to the following picture, there is no session layer, or it is skipped:



Source: http://www.protocols.com/pbook/tcpip1.htm

Curious enough, there is nothing in the presentation layer.

Linkedin's wall of fame/shame

Linkedin gives me a list of all the people that have done something very stupid.
They have given their email passwords to the "most weird social network":

Screen Shot 2013-10-23 at 8.19.28 PM

Note: in my early days I may have done the same - I don't remember, but if I did - I was also stupid to do so. I have checked several times if linked has some access to my email - via an app or via some other means - it doesn't.

Bootable USB with OS X Mountain Lion

Find the InstallESD.dmg
- either from App Store (Start installing Maverics (even if you are already on Maverics) and the location to the file is: /Applications/Install OS X Mavericks.app/Contents/SharedSupport/InstallESD.dmg
- you can also extract it from the Recovery Partition

Note: do not use the iso version and don't try to convert it. You will fail.

Then download http://liondiskmaker.com/ and follow the instructions.

highlight code (xml, javascript, java, php, c#....) in html

Screen Shot 2013-08-19 at 2.39.59 AM

How can one highlight (use a code color scheme) for source code or xml or html and do the following and do it in any html page?
Like this:
Screen Shot 2013-08-19 at 2.38.24 AM

One can use a javascript library. Instead of explaining how it works, here's the trick:

  <pre name="code" class="xml:nogutter:nocontrols">
    <?xml version="1.0" encoding="UTF-8"?>
      <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
                      <news:name>Kenosha News</news:name>
                <news:title>On target: Archer sets county fair record</news:title>
  <link type="text/css" rel="stylesheet" href="http://syntaxhighlighter.googlecode.com/svn-history/tags/1.5.1/Styles/SyntaxHighlighter.css" />
  <script language="javascript" src="http://syntaxhighlighter.googlecode.com/svn-history/tags/1.5.1/Scripts/shCore.js"></script>
  <script language="javascript" src="http://syntaxhighlighter.googlecode.com/svn-history/tags/1.5.1/Scripts/shBrushXml.js"></script>
  <script language="javascript">

The important part is:


xml = the language
nogutter, nocontrols = options
http://syntaxhighlighter.googlecode.com/svn-history/tags/1.5.1/ = I don't want to include these in my project, so I use them remotely, you can download them locally, but download all.
Add the js libraries for the languages you use. For xml I added shBrushXml (see the code).

Supported languages: https://code.google.com/p/syntaxhighlighter/wiki/Languages
Configuration options: https://code.google.com/p/syntaxhighlighter/wiki/Configuration