Rough Book

random musings of just another computer nerd

Category: Nerdy Stuff

Comments were broken for a while

Commenting on this blog was disabled for a while… I think it was due to Akismet misbehaving or something. I’m not sure. Either way, you should be able to comment now. I will be slowly adding anti-spam stuff back in.

The image that lied about itself

A few weeks ago, I ran into a puzzling issue at work. Someone was uploading an image which made it past our file-size checks, but caused an OutOfMemoryError and a heap dump when the code attempted to resize it. The information we had from the heap-dump was that it was trying to allocate memory for an image whose resolution was 18,375×11,175. What didn’t make sense is how this image was even getting through our file-size checks, because there is no way we would ever let in an image of that size.

In the code, we have a global limit for the largest file we will accept. We also have a separate limit for image uploads. If the image is over this size, but under the global limit, we will resize the image to a smaller size. The strange part was that the large image was making it past the global check, which meant that the size of the incoming data was below the global limit, but above the image-size limit. How could this be?

On a hunch, I hypothesized that perhaps the entire image wasn’t making it through. Perhaps only a part of the image was making it through with its header left intact. In an image file, there is usually a header that conveys information about the file format, the color space, and the resolution of the image! I figured that even though the data are incomplete, enough information was present in the header to enable the resizing code to make sense of it. When it tries to allocate memory for this image, based on the resolution it gets from the header, it runs out of memory!

To verify this, I manually hex-edited a file that was of proper size to have a ridiculously-large resolution. I then uploaded this file and was able to witness the behavior happening even though the file was of the proper size! So what’s the lesson here? Don’t only rely on file-size limits for images; you have to look at the resolution as well!

Maintaining a sorted array in O(1)

Yesterday, I came across an interesting question on StackOverflow. The question is as follows: assuming you have a sorted array, is it possible to increment locations by 1 (one at a time) and still ensure that the array is sorted in O(1)? Assuming you are not allowed to use any other secondary data-structures, the answer is no. This becomes evident when you have repeated values. Assume a degenerate case where you have an array of identical values; let’s say [0, 0, 0, 0, 0]. If you increment the value in location 0, you now have [1, 0, 0, 0, 0]. For this array to be sorted, the value 1 must be moved to the end of the array (index 4). This will require four comparisons, which means that this will inevitably turn into an O(n) algorithm.

How can we do better?

Read the rest of this entry »


I am at JavaOne! The last time I was here was in ’08, when it was run by Sun. Of course, it’s run by Oracle now. The first day has been pretty good. I attended sessions on Garbage Collection, the Nashorn JavaScript engine, writing DSLs, and about parallelization options offered by JDK 7 and 8 to leverage multicore processors. Pretty good first day!

Nashorn looks pretty interesting and I will be giving it a closer look when I come back home. They are also looking for people to help out so I am going to see if I can contribute anything useful.

Today I will be checking out a talk on Lambdas by Brian Goetz and will also be going to a talk on Big Data. Finally I also have some BOF (birds of a feather) sessions on evolutionary algorithms and writing parsers in Scala. Pretty interesting day and I’m looking forward to it!

How to stop YouTube sucking on Ubuntu/Linux

Recently I’ve noticed that YouTube’s performance on my machines have been terrible. It’s constantly buffering, or it will stop randomly in the middle of a video. I’ll get a few seconds of playback and then 10-30 seconds of buffering. It’s pretty terrible. On Windows I have been able to use the helpful workaround from here and performance has definitely increased. On top of that, I’m also using the SmartVideo plugin on Chrome (it’s also available for FireFox). But on my Linux boxes, I’m still having the same problem in spite of having the SmartVideo plugin. There is a Linux alternative to guide from above, but it uses ipfw program which is not natively available on Ubuntu/Linux (at least from my understanding) due to it being a BSD program. I didn’t want to compile it and install it from source, so I decided to use ufw instead, which is the “Uncomplicated Firewall” that comes with Ubuntu. It was pretty simple to convert the rules over. But first you will need to enable it (if you haven’t already). You can do that with:

sudo ufw enable

Then you can enable logging also, if you want:

sudo ufw logging on

If you SSH into your machine or if you use your machine as a webserver, you will need to enable a few more rules:

sudo ufw allow ssh/tcp
sudo ufw allow http/tcp
sudo ufw allow 8080/tcp

And of course, you can add the rules that will prevent your ISP from caching YouTube:

sudo ufw deny from
sudo ufw deny from

You can then use ufw status to verify that your rules are in place:

 ~ ⮀ $ ⮀sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
8080/tcp                   ALLOW       Anywhere
Anywhere                   DENY
Anywhere                   DENY
22/tcp                     ALLOW       Anywhere (v6)
80/tcp                     ALLOW       Anywhere (v6)
8080/tcp                   ALLOW       Anywhere (v6)

Rendering a PDF with text-selection, using pdf.js

I have been working a project for the last few days, that deals with rendering PDF’s in-browser. Initially, I was going to parse the PDF and extract the text content, but then I ran into pdf.js, which is a library developed by Mozilla for rendering PDF’s in-browser via JavaScript. The project I am working on has a requirement that users should be able to select text within the PDF. This is possible using pdf.js. Unfortunately, the example code only shows you how to render a PDF, but not how to enable text-selection. I wasn’t able to find any API access to enable text-selection either. I finally ended up on the #pdfjs IRC channel and the friendly folks there gave me some direction. The logic for enabling text-selection was buried inside the code for Mozilla’s PDF viewer, and was heavily intertwined with the viewer code as well. I spent a few days playing around with the viewer and tracing through the code. I was stumped many times since the code was complex and I know jack about parsing PDF’s. But eventually I was able to focus on the part of the code that actually took care of enabling text-selection.

pdf.js’ approach to enabling text-selection is actually quite clever. The library overlays divs over the PDF, and these divs contain text that matches the PDF text that they are floating over. So when you select the text, you are actually selecting the text inside the overlaid divs. This was fine and dandy, but I was still stuck as far as getting this to work on my project. What I needed was a minimal example that I could adapt for my uses. After a day or two of tracing code, experimenting, debugging, and staring at the screen in frustration, I was eventually able to come up with a minimal example! To accomplish this, I extracted code that was relevant to creating the overlays out of the viewer code, into its own independent file. I also removed a lot of code that was dependent on the viewer itself. Keep in mind that this example doesn’t have functionality like text finding or matching, and that code is also heavily intertwined with the viewer code. All this example does is render a PDF with text-selection enabled. However, I think this is a good start!

If you are interested, you can check out the code on github and a working example on this fiddle.

The pertintent code is as follows (keep in mind you still require additional resources; all of that information is available on github):

window.onload = function () {
    var pdfBase64 = "..."; //base64 representing the PDF

    var scale = 1.5; //Set this to whatever you want. This is basically the "zoom" factor for the PDF.

     * Converts a base64 string into a Uint8Array
    function base64ToUint8Array(base64) {
        var raw = atob(base64); //This is a native function that decodes a base64-encoded string.
        var uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
        for (var i = 0; i < raw.length; i++) {
            uint8Array&#91;i&#93; = raw.charCodeAt(i);

        return uint8Array;

    function loadPdf(pdfData) {
        PDFJS.disableWorker = true; //Not using web workers. Not disabling results in an error. This line is
        //missing in the example code for rendering a pdf.

        var pdf = PDFJS.getDocument(pdfData);

    function renderPdf(pdf) {

    function renderPage(page) {
        var viewport = page.getViewport(scale);
        var $canvas = jQuery("<canvas></canvas>");

        //Set the canvas height and width to the height and width of the viewport
        var canvas = $canvas.get(0);
        var context = canvas.getContext("2d");
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        //Append the canvas to the pdf container div
        var $pdfContainer = jQuery("#pdfContainer");
        $pdfContainer.css("height", canvas.height + "px").css("width", canvas.width + "px");

        //The following few lines of code set up scaling on the context if we are on a HiDPI display
        var outputScale = getOutputScale();
        if (outputScale.scaled) {
            var cssScale = 'scale(' + (1 / + ', ' +
                (1 / + ')';
            CustomStyle.setProp('transform', canvas, cssScale);
            CustomStyle.setProp('transformOrigin', canvas, '0% 0%');

            if ($textLayerDiv.get(0)) {
                CustomStyle.setProp('transform', $textLayerDiv.get(0), cssScale);
                CustomStyle.setProp('transformOrigin', $textLayerDiv.get(0), '0% 0%');

        context._scaleX =;
        context._scaleY =;
        if (outputScale.scaled) {

        var canvasOffset = $canvas.offset();
        var $textLayerDiv = jQuery("<div />")
            .css("height", viewport.height + "px")
            .css("width", viewport.width + "px")
                left: canvasOffset.left


        page.getTextContent().then(function (textContent) {
            var textLayer = new TextLayerBuilder($textLayerDiv.get(0), 0); //The second zero is an index identifying
            //the page. It is set to page.number - 1.

            var renderContext = {
                canvasContext: context,
                viewport: viewport,
                textLayer: textLayer


    var pdfData = base64ToUint8Array(pdfBase64);

A simple trie-implementation

I’ve known about tries for sometime. They’re a pretty neat data-structure for storing and looking-up strings.I decided to try and implement one in Java so that I can learn more about them. I’ll post another article later that goes into some more detail about this implementation, but for now you can check out the source here. It’s not production-ready by any means; it’s just me playing around.

Explanation of the dialup sound

Ran into this awesome explanation of the dialup protocol, i.e., the sound you hear from a modem when it dials up.

The Feast

Inspired by The Codeless Code:

The master was meditating when a priest respectfully entered his chamber. The master opened his eyes. The priest bowed respectfully and said, “Master, I would like you to look at the code of a young disciple of mine”. The master nodded and followed the priest to a computer. On the screen, was a code listing. The priest pointed to the code and said:

“My disciple created an abstract class and another class that extends the abstract class. However, he has a method that should be of use to all future derivations of the abstract class.”

The master furrowed his brow and looked at the code. “Indeed.”, he said. The priest continued, “I pointed this fact out to him and mentioned that it would be better to define it in the abstract class”. “I see; what did he say?”, asked the master.

The priest sighed and said, “He said ‘You Ain’t Gonna Need It’ and that he could simply copy the method into each new derivation when the time comes”. The master then asked the priest to bring the disciple to him at once. The priest bowed and went away to fetch the young disciple.

A few minutes later, the young disciple respectfully entered the master’s chamber. “You sent for me, master?”, he said. “Yes. I have a task for you”, said the master. The disciple bowed, indicating that he was ready to perform whatever task the master required. The master looked at the disciple and said: “Tomorrow, we will have monks visiting from a neighboring monastery. In their honor, our monastery is providing a feast for them. I need you to report to the dining hall tomorrow. The cook will give you further instructions”. The disciple bowed and left the master’s chamber.

The next morning the disciple arrived at the dining hall as he was asked. He looked around and noticed a large number of seats. He assumed, correctly, that these seats were for the visiting monks. He then noticed the monastery’s cook approaching him. The cook was holding a bowl that was filled with a white substance. As the cook got closer, the disciple realized that it was salt. Once the cook reached the disciple, he held out the bowl to him and said, “Master has asked you to give salt to any of the monks who desire it”. The disciple took the bowl and the cook left. The disciple was puzzled, but smiled thinking that this was going to be an easy task. After all, how many monks would require more salt in their food? He estimated only a few; not much more than that.

In a few minutes, the visiting monks arrived and sat at their places. Other monks from the hosting monastery brought out steaming bowls of soup for the visitors. The head visiting-monk took a spoonful of soup, sipped it and wrinkled his nose. “This soup does not have any salt!” he said, rather loudly. The disciple quickly ran up to the head visiting-monk and bowed and said “Master, I apologize that there is not enough salt in your soup, please allow me to offer you some!” The head monk nodded and the disciple quickly added salt until the monk motioned him to stop. He had barely finished when he heard another monk complaining that there was no salt in his soup either, then another, and another. Soon he was running around the hall at full speed, bringing salt to each of the visiting monks, trying his best to make sure that everyone was happy. The hall was big, and the number of visiting monks was many. When he was done, he sat down exhausted, in the corner of the hall.

He closed his eyes and tried to catch his breath. When he opened his eyes, he noticed that the next course was being brought in. “Surely the cook wouldn’t have forgotten to add salt this time!”, he thought. Unfortunately, it was not so! “There is no salt in my meal!”, thundered the head visiting-monk. The disciple got up and ran to the head monk to add salt his meal. Soon other monks started complaining and the disciple was running around the hall as he had done before, offering salt to all the visiting monks. The disciple hoped that this would be the last time he would have to do this, but alas, there were three more courses! The rest of the courses passed by in a blur for the disciple. All he could remember was that he was running around the entire hall, bringing salt to the visiting monks. The monks didn’t all finish their meals at the same time and so the later courses were continuously being brought out. Hence, he was always on his feet and didn’t get a chance to rest.

Finally, the monks stopped asking for salt and the disciple wearily went back to his corner. Dessert would be next, and there would be no need for salt then. He had barely sat down when he saw the cook approaching him. This time he had another bowl, also filled with a white substance. When the cook got close, he offered the bowl to the disciple and said a single word: “Sugar”. The disciple was almost in tears. He knew was was coming and prepared himself for the endless rushing around that he would have to do. Luckily there was only one course of dessert. When the feast was done, the disciple collapsed in the corner. He opened his eyes and noticed the cook walking towards him again. “What more will he want me to do?”, thought the disciple frantically. He noticed with some relief however, that the cook’s hands were empty. “Master will see you now”, said the cook as he got closer to the disciple. The disciple wearily got up to his feet and walked to the master’s chambers.

After a few minutes, the disciple arrived at the master’s chambers. He walked in and bowed in front of the master, his legs burning with fatigue. “How was your task?” asked the master with his eyes still closed. “It was exceedingly difficult master! There was no salt in the soup or the meals, and no sugar in the desserts! I had to run around the whole hall bringing salt and sugar to the visiting monks!”, said the disciple. “It must have been exceedingly tiring…”, said the master. “Yes, master! It was!”, said the disciple nodding his head. The master opened his eyes and said, “One could say that your task would have been much easier had the salt and sugar been added to the meals at the source, and thus before they were brought out to our honored visitors.”

In that moment, the disciple was enlightened.

Data and Code

Inspired by The Codeless Code:

A novice monk had just started learning assembly programming when he was troubled by doubt. He approached his master and asked:

“Master, how do I know which is code and which is data?”

The master who was meditating, opened his eyes, smiled, and said:

“Each is the other, yet neither is either.”

“Master, I do not understand.”, said the disciple.

The master then brought out two identical pots and said. “Take these. Fill one with the water from the lake, and fill the other with water from the stream that flows into the lake. Then bring them to me.”

The monk bowed and took the pots. He walked to the lake, which was some distance away and filled it with water from the lake. Then he walked around the side of the lake until he found the stream that fed water into the lake. He used this water to fill the second pot and then brought both pots back to his master and set them at his feet. The master looked at the pots, and then back to his disciple and said “Now go. You may come back tomorrow morning.”

The disciple came back the next morning to find his master standing with the two pots. He held up both the pots and then threw them to the opposite sides of the room. The pots smashed, and the water from both pots flowed towards the center of the room, forming a puddle.

The master then said, “Which pot contained water from the stream? Which pot contained water from the lake?” He then pointed to the growing puddle that was forming in the middle of the room. “Which part of the puddle contains water from the stream? Which part contains water from the lake?”

In that moment, the novice was enlightened.

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: