Java: adding directories in a ZipOutputStream

Lately I had to write some Java code that consumes and generates zip files using the classes from the java.util.zip package. Although I wasn’t excited with the library, it is fairly simple and gets the job done. Here’s a very simple example:

FileOutputStream fos = new FileOutputStream("myfile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry("myfile.txt"));
// write file's contents to zip stream
zos.closeEntry();
zos.close();

One small problem I faced was when I needed to add a directory entry in my output zip. The ZipEntry class has a isDirectory() method which is useful when reading entries from an ZipInputStream, however there is no setDirectory() method to explicitly specify that an entry refers to a directory.

The solution may be quite intuitive although it may not be very obvious: to specify a directory entry we simply have to add a '/' character at the end of the name:

FileOutputStream fos = new FileOutputStream("myfile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry("mydir/"));
zos.putNextEntry(new ZipEntry("mydir/myfile.txt"));
// write file's contents to zip stream
zos.closeEntry();
zos.close();

I hope this helps! :-)

Posted in Uncategorized | Leave a comment

Arduino: properly measuring time intervals

When programming an Arduino it is sometimes useful or necessary to measure the time that elapsed between two certain points of the program’s execution. The most straightforward way to do that is to save one timestamp on each point, and then subtracting the two timestamps will yield the time interval.

If precision is not a big issue, one can use the millis() function which returns the number of milliseconds that have been elapsed since the Arduino was powered up. For example:

  uint32_t ts1 = millis();
  // ...TASK TO BE MEASURED GOES HERE
  uint32_t ts2 = millis();

  // print the time interval in milliseconds
  Serial.println(ts2-ts1);

Otherwise, if  greater precision is required, one can use the micros() function which returns likewise the number of microseconds since the Arduino was powered up (with a precision of 4 or 8 μsec, depending on the clock speed of your Arduino). Our example code becomes now:

  uint32_t ts1 = micros();
  // ...TASK TO BE MEASURED GOES HERE
  uint32_t ts2 = micros();

  // print the time interval in microseconds
  Serial.println(ts2-ts1);

However the examples above, although simple and straightforward, work most of the time but not always. The reason is that the registers that count both milli- or microseconds since the Arduino’s boot up have a limited capacity of 32 bits. That means that when they reach their highest possible value (0xffffffff, or equally 4294967295) they will overflow and start all over from zero.

If the overflow occurs anywhere outside the two calls to millis() or micros(), the code above will work fine, because ts2 will be greater than ts1 and the result of the subtraction will be correct.

On the other hand, if the overflow occurs between the first and the second call to millis() or micros(), then there is a small problem. ts2 will not be greater than ts1 anymore, and simply subtracting ts2ts1 will yield a wrong result.

The overflow of the counters occurs once every 49.7 days for the milliseconds counter, but in the case of the microseconds counter this happens much more often: every 71.5 minutes! In any case, it may not be always possible to tolerate a wrong calculation, even if it occurs seldom. However this can be worked around!

We can still calculate the time interval between those two timestamps as the sum of two sub-intervals: dt1 which is the interval between t1 and the overflow point (timestamp = 0), and dt2 which is the interval between 0 and t1. Then the total interval dt would be:

  uint32_t dt = dt1 + dt2;

Calculating dt2 is fairly easy: it’s just t10, or simply t1:

  uint32_t dt2 = t1;

Calculating dt1 is a bit more complicated, and requires to invert all the bits of t2, and then add +1 to it. This is required because if we just invert the bits we get the interval between t2 and 0xfffffff. We need to add +1 to get the actual interval between t2 and 0:

  uint32_t dt1 = 1 + ~t2;

Wrapping everything together looks like this:

  uint32_t dt1 = 1 + ~t2;
  uint32_t dt2 = t1;
  uint32_t dt = dt1 + dt2;

Which can be simplified to:

  uint32_t dt = 1 + t1 + ~t2;

Now all we have to do is to discriminate the two cases:

  uint32_t dt = t1 > t2 ? 1 + t1 + ~t2 : t2 - t1;

The statement above checks if t1 > t2 or not, and then performs the appropriate calculation. I hope this was useful! :-)

Posted in Uncategorized | Leave a comment

Netbeans slow repaint over Remote Desktop [solution]

Lately I have been connecting sometimes to my computer at work, from home via Remote Desktop Connection (RDP). While everything is pretty much acceptable, I have noticed that Netbeans was so slow in repainting its windows, to the point of becoming unusable. With other IDEs and other software in general I never had a similar issue. If you are experiencing the same problem, here is how to fix it.

Go to the installation directory of your Netbeans (that would be on Windows probably under C:\Program Files\NetBeans 8.0), and then go to the etc folder and look for a file named netbeans.conf. Open this file in an editor and search for the text “noddraw” (with two ‘d’). You should probably see that this property has the value ‘true’ set to it:

Change the value of the property to ‘false’ and save the file. Then restart Netbeans. Repainting must be faster now. Good luck!

Posted in Uncategorized | Leave a comment

Unicode characters dropped in PDF files generated with iText and Flying Saucer

Flying Saucer is a very useful Java library that uses iText to convert HTML pages to PDF documents. Here is a nice tutorial on how to use Flying Saucer.

The last few days I was trying unsuccessfully  to generate some report that contained non-standard Unicode characters (in my case it was Greek, but I guess the same problem exists for other character sets as well, like Cyrillic, Armenian, etc). The problem was that the Greek characters seemed to be omitted; they didn’t show up in the document.

The code I was using was more or less something like that:

public class Html2Pdf {

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File("output.pdf");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("input.html"));
        document.close();
        Desktop.getDesktop().open(file);
    }
}

And the input HTML file was something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <h1>Αρνάκι άσπρο και παχύ</h1>
    </body>
</html>

When I tried to convert this simple HTML to PDF, I got a blank page.

After lots of hours of troubleshooting, I finally discovered that, for some reason, if no specific font is used, the generated PDF uses some kind of  default (probably Helvetica) font, that contains a very limited character set, that obviously does not contain the Greek code page.

So I came up with this simple trick, that seems to solve the problem. I only had to make sure that all elements in my HTML file will use a font that contains Greek, like Arial:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    <head>
        <style>
        * { font-family: Arial; }
        </style>
    </head>
    <body>
        <h1>Αρνάκι άσπρο και παχύ</h1>
    </body>
</html>

Arial is a pretty standard font, installed by default in most operating system, and implements a wide variety of alphabets (including Greek).

I hope this helped…

 

Posted in Uncategorized | Leave a comment

MySQL: select the n-th element from a delimiter separated value set

Lately I’ve been dealing with a database where a lot of values are stored as strings of concatenated values, separated by delimiters (e.g. “1,2,3,4″). I have to use these values in queries, for example, for joining with other tables. Selecting a specific value from a list of delimiter separated values can be a bit tricky.

In the beginning I was trying to do that using the SUBSTR and LOCATE functions of MySQL. This worked very well when I had only two values in the string. For example:

mysql> SET @values = 'Cat#Dog';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTR(@values,1,LOCATE('#',@values)-1) `1st value`;
+-----------+
| 1st value |
+-----------+
| Cat       |
+-----------+

mysql> SELECT SUBSTR(@values,LOCATE('#',@values)+1) `2nd value`;
+-----------+
| 2nd value |
+-----------+
| Dog       |
+-----------+

But if you have more than two values in your string, things start to get more complicated. For example, here’s how to get the 2nd value from a 3-value string:

mysql> SET @values = 'Square#Triangle#Hexagon';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTR(@values,LOCATE('#',@values)+1,LOCATE('#',
       SUBSTR(@values,LOCATE('#',@values)+1))-1) `2nd value`;
+-----------+
| 2nd value |
+-----------+
| Triangle  |
+-----------+

I thought I should find a way to generalize this so I can get the n-th value of N-value strings, but it was obvious that it was going to get unreadable and unmanageable very soon.

Then I found about an interesting MySQL function called SUBSTRING_INDEX(). According to MySQL’s documentation it “returns a substring from a string before the specified number of occurrences of the delimiter”, which at least to my ears sounds a bit obscure… After doing a couple of tests it was very clear to me to understand what it does:

mysql> SET @values = '1#2#3#4#5#6#7#8#9#10';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTRING_INDEX(@values,'#',4);
+--------------------------------+
| SUBSTRING_INDEX(@values,'#',4) |
+--------------------------------+
| 1#2#3#4                        |
+--------------------------------+

mysql> SELECT SUBSTRING_INDEX(@values,'#',-3);
+---------------------------------+
| SUBSTRING_INDEX(@values,'#',-3) |
+---------------------------------+
| 8#9#10                          |
+---------------------------------+

This function takes a string of values, a delimiter and a number n that specifies a position (or index). This index can be either positive or negative. If it is positive, the function returns the portion of the string that contains the first n values. If it is negative, it returns the portion of the string that contains the last ABS(n) values.

That was all I needed to write a generic piece of code that would return the n-th value. First one call SUBSTRING_INDEX to get the portion of the string that contain the first n values, and then a second call to get the last value from the subset!

In this example we extract the 4th value from a string containing animals:

mysql> SET @values = 'cat#dog#horse#parrot#gecko';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@values,'#',4),'#',-1);
+--------------------------------------------------------+
| SUBSTRING_INDEX(SUBSTRING_INDEX(@values,'#',4),'#',-1) |
+--------------------------------------------------------+
| parrot                                                 |
+--------------------------------------------------------+

In general, to select the N-th value from a string VALUES that contains values separated by delimiter DELIM, you have to use:

SUBSTRING_INDEX( SUBSTRING_INDEX( VALUES, DELIM, N ), DELIM, -1 )

I hope this helped! :-)

 

Posted in Uncategorized | 4 Comments

NetBeans: how to bring GUI Builder Assistant back

NetBeans IDE comes with a very hand visual editor for designing Forms and Dialogs, called the “GUI Builder“. One of the features I like the most is the ability to snap components relative to another component’s position or relative to the container component’s edges, etc. However, if you work on a complex form with many different components around it is possible that things may get confusing regarding what is snapping where.

To work this around the GUI Builder provides the GUI Builder Assistant. This is the white box with the bulb icon on the left. During snapping this box gives enlightening information about how the moving component will be positioned, by displaying messages like: “Snap at the preferred distant from the container’s left border”, “Snap at the small preferred distance from a component above”, etc.

This GUI Builder Assistant tool comes with a close (‘X’) icon which allows you to easily hide it if you don’t need it. But if you accidentally clicked the close icon, it may be confusing to restore the assistant back. To do so, you have to go to the Tools menu and open Options dialog. From there go to the Java section, the select the GUI Builder tab, and make sure that you check the Show Assistant checkbox.

(In a pinch, you have to go to: Tools -> Options -> Java -> GUI Builder -> Show Assistant).

I hope this helps!

 

Posted in Uncategorized | Leave a comment

jQuery: How to check whether a div contains hidden “overflown” content?

The “overflow: hidden” CSS property instructs the browser to clip the excessive content that appears inside an element. One way to detect programmatically (in Javascript) whether an element actually contains any “clipped” content or not, is to compare the value of the “scrollWidth” (the total width of the content, including any clipped parts), against the value of the net width of the element (that is, the width of the element freed from any eventual border widths).

The first value can be directly obtained using the Javascript’s scrollWidth property. The latter can be calculated using jQuery’s innerWidth() method. Therefore, the code should more or less look like this:

        if (element.scrollWidth > $(element).innerWidth()) {
            // Contains hidden content!
        } else {
            // All content visible!
        }

I hope this helps!

Posted in Uncategorized | Leave a comment

How to constrain an image's dimensions while maintaining its ratio

I know, this is not nuclear physics, but it tricks my mind every once in a while when this occurs to me.

Let’s assume the general case of an image with dimensions w×h, and that you need to scale it down so that it fits within a certain rectangle sizing w0×h0, while its sides maintain the same ratio R=w/h.

In other words, we need to calculate a new pair of dimensions w’×h’ for our image, for which the following conditions must be satisfied:

(1) w’/h’ = w/h = R

(2) w’ ≤ w0

(3) h’ ≤ h0

Note that that the ratio of the constraining box w0/h0 is irrelevant to the ratio of the image R.

From the first of the above conditions (1) we get:

w’/h’ = w/h ⇒ w’·h = w·h’   w’/w = h’/h = φ

which means that both the width (w) and the height (h) of the image must be scaled by the same factor φ; thus the problem is pretty much reduced to simply calculating the scaling factor φ.

Let’s take it a bit further by combining the last finding with the inequations (2) and (3):

w’/w = h’/h = φ ⇒ w’ = w/φ , h’ = h/φ  ⇒ w/φ ≤ w0 , h/φ ≤ h0  ⇒ w/w0 ≤ φ , h/h0 ≤ φ .

From the last statement it is clear that all we need to do is to choose φ = max[ w/w0h/h0].

To summarize, these are the steps we need to take:

1)  φ = max[ w/w0h/h0]

2)  w’ = w/φ

3) h’ = h/φ

Or, if you please, the conditional equivalent:

Is w/wh/h0 ? 

then:  w’ = w0 , h’ = w0·h/w

or else:  w’ = h0·w/h , h’ = h0

Posted in Uncategorized | Leave a comment

How to arithmetically sort an array in Javascript

Something interesting that I discovered tonight is that JavaScript’s sort() function by default sorts values lexicographically (that means, for example, that “10″ comes before “2″), even when the array contains numerical data.

Let’s consider the following code:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort();
console.log(values);

The above code yields:

[1, 10, 100, 20, 3, 4]

However, in case we need to sort those values as numbers, it is possible to pass a “comparator” function as an argument to sort(), and this function will be used by sort() to decide the order of the elements. To sort arithmetically the array in the above example, we will have to do it like this:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort( function(a,b) {
    return a-b;
});
console.log(values);

Now the code above yields:

[1, 3, 4, 10, 20, 100]

For reverse sorting we just have to swap the two operands in the sorting function:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort( function(a,b) {
    return b-a;
});
console.log(values);

Result:

[100, 20, 10, 4, 3, 1]

One thing to notice is that above code even works if the values are stored as strings; that’s because the values of the operands are implicitly converted to integers by the subtraction operator (‘-’):

var values = [ '1', '10', '100', '4', '20', '3' ];
values.sort( function(a,b) {
    return a-b;
});
console.log(values); 

Result:

["1", "3", "4", "10", "20", "100"]

If we would prefer to be more “bullet-proof” when dealing with numbers stored as strings, we could use the parseInt() function to make sure we are really dealing with integer values.

var values = [ '1', '10', '100', '4', '20', '3' ];
values.sort( function(a,b) {
    return parseInt(a)-parseInt(b);
});
console.log(values);

Result:

["1", "3", "4", "10", "20", "100"]

Notice that the sorted values remained stored as strings.

So, that was pretty much all of it, I hope this helped! :-)

 

Posted in Uncategorized | Leave a comment

PHP: Something to watch out when using global scope variables, or "why globals doesn't (seem to) work!?"

Okay, I guess this is probably a childish mistake for an experienced PHP-er, but I myself got busted tonight, and maybe I’m not the only one :-) So, here it is.

Consider the following small PHP program:

<?php
print_global_a();

$a = 10;

function print_global_a() {
        global $a;
        echo "$an";
}

What would be the output of this code? What?? 10?? Wrooong! Let’s save this as global.php and run it, see what happens.

giannis@ubuntu:~$ php global.php

giannis@ubuntu:~$

It outputs an empty new line… Okay, then maybe something wrong with the ‘globals’ keyword. Let’s try the $GLOBALS array instead:

<?php
print_global_a();

$a = 10;

function print_global_a() {
        echo $GLOBALS['a']."n";
}

Now let’s run it again:

giannis@ubuntu:~$ php global.php
PHP Notice:  Undefined index: a in /home/giannis/global.php on line 7

giannis@ubuntu:~$

It just got even worse; not only we still get an empty new line, we also get a big fat warning.

So, what is really wrong here? It is so simple, you probably noticed on the first glance, but if you didn’t, here it is: At the moment that the function is called the $a global variable is not yet set. Therefore, when we try to access it from within a function, we simply cannot, not because ‘globals’ is not working, but because it is not set in the first place. The fact that the definition of the function comes after the assignment may delude us to think that the variable will be set when the code in the function is executed.

So, in other words, the next time you may face a “globals” that doesn’t seem  to work, make sure that the variable is actually set at the point where the function is called.

Unfortunately, PHP is a tool that does not protect the programmer from doing nasty mistakes… I hope you found this interesting and helpful :-)

Posted in Uncategorized | Leave a comment