New Website: Linux Programming Forums

| No Comments
I have started a new web site:

linuxprogrammingforums.com

The web site provides online forum resource for a wide variety of languages and programming topics. The layout and organization of the forums is geared toward the needs and mindset of programmers with a Linux background.

The board motto is

"May the source be with you."

Java BufferedImage: Beware of getSubimage!

| No Comments
Wow, I just spent way too long trying to figure out a bug in some of my gaming code. Nothing made sense no matter how many times I looked at the problem and how many angles I considered. The weirdest things were happening in my graphical output, and I was getting very frustrated.

I got desperate and decided to look over the APIs of all the graphical functions I was using. Then I looked at the BufferedImage.getSubimage function API and it was painfully obvious:

getSubimage

public BufferedImage getSubimage(int x,
                                 int y,
                                 int w,
                                 int h)

    Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.

    Parameters:
        w - the width of the specified rectangular region
        h - the height of the specified rectangular region
    Returns:
        a BufferedImage that is the subimage of this BufferedImage.
    Throws:
        RasterFormatException - if the specified area is not contained within this BufferedImage.

I had assumed that getSubimage would return a new BufferImage array, but it actually is really just a map to part of the original BufferImage array. Consequently, whenever I was painting in the new BufferImage object, I was actually painting all over my original BufferImage!

Well, that was a lesson learned the really hard way.

Java: Making Ambiguous References Unambiguous

| No Comments
// *** Key.java ***
interface Key {}

// *** Card.java ***
interface Card {}

// *** KeyCard.java ***
public class KeyCard implements Key, Card {}

// *** Door.java ***
public class Door
{
    public void openWith(Key key) { System.out.println("Opened door with key"); }
    public void openWith(Card card) { System.out.println("Opened door with card"); }
}

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
door.openWith(keycard);
    }
}

What's wrong with this Java code? It won't compile:

$ javac Scenario.java
Scenario.java:15: reference to openWith is ambiguous, both method openWith(Key) in Door and method openWith(Card) in Door match
door.openWith(keycard);
^
1 error

There are two functions called openWith, which is fine, because Java supports overloaded functions. And we are passing an object to a function on the basis of which interface it implements, which is fine, because Java supports polymorphism. We have also implemented more than one interface in the door class, which normally is fine, because Java allows you to implement more than one interface per class, as a replacement for multiple inheritance.

The problem, though, is that in this case we have an overloaded function, openWith, capable of taking an object that implements either the key interface or the card interface, but we passed it an object that implements both the key interface and the card interface, and the compiler does not know what you want it to be passed in as. So it is an ambiguous function call.

I ran into this error in some code at work today. (Real code, not the silliness above.) I wasn't quite sure what to do. One option was to change the names of the functions in the Door class, to something like openWithKey(Key key) and openWithCard(Card card). But that would be effectively abandoning function overloading, which is a really convenient and tidy feature in any language.

Another option would be change the KeyCard class to support only one interface or the other. But I want KeyCard to have more than one interface implemented... that is what makes it such a cool object.

It took me a few minutes of staring at my screen to recognize the simplest, easiest, and most obvious choice. Every object variable in Java is really just a reference to the object right? So why don't we just add a few lines to the Scenario class, like so?:

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
Key key = keycard;
Card card = keycard;
door.openWith(key);
door.openWith(card);
    }
}
Now this compiles and runs:

$ javac Scenario.java
$ java Scenario
Opened door with key
Opened door with card

The variables key, card, and keycard all refer to the same object. That's fine with the compiler... it just needs to know which interface the object is supposed to look like when you pass it in.

So, in summary, make an ambiguous function call unambiguous by aliasing it before-hand to be the specific kind of interface you need at that moment.

First Gentoo Router

| No Comments
I stayed up late last night setting up my first Gentoo-based home router out of an old PC (500 Mhz Pentium III, 256 MB RAM). It has three interfaces (two LAN, one WAN), is fully optimized for the processor, and provides DHCP, DNS, and NAT services.

It was overall fairly easy following the online Gentoo documentation:

http://www.gentoo.org/doc/en/home-router-howto.xml

The longest part was all the time I spent trimming down the kernel (not strictly necessary but a lot of fun) and then it takes a long time for the kernel to build on a 500 Mhz box. It took probably 3-4 of my attention, including putting the PC together and testing the services, and about 7+ hours of computer time.

Uh, no... Very Different.

| No Comments
So I'm sitting on some stair steps at school, and I turn my laptop on, which is running Gentoo. The boot phase starts as usual, and the text scrolls by as the services start up.

A large, young fellow walks by, sees the screen, and says "Is that MS-DOS?"

"No," I quickly reply, "It's Linux."

"Oh. They're basically the same, aren't they?" he says.

Ouch, that hurt.

Nifty! Custom-Built Linux Computers

| No Comments
system76

What attracted my attention was the server section, where you can by tower or rack mounted servers starting at around $700. Everything is Linux compatible, and they give you lots of options on the components.

After playing around with the "Eland Pedestal" tower server, I found that for about $1,200 I could get something like this:

  • Quad Core Intel Xeon X3230 2.66 GHz 1066 MHz FSB 8 MB L2 65nm
  • 4 GB DDR2
  • 3x 500 GB drives in a RAID 5 array

Wouldn't mind having two or three of those babies for my cluster.

Simple C++ Wrapper Around LibcURL

| No Comments
I wrote a very simple (one function) C++ wrapper class around libcurl, which one can use to download web page data into a C++ string. The class is called WebPageRetriever and is available here:

http://indicium.us/cgi-bin/pages/get?view=scripts

What Happens When Linux Runs out of Memory?

| No Comments
What does your Linux system do when it completely runs out of memory? When even virtual memory is exausted?

As a last resort, Linux turns your processes over to a dark angel known as the OOM Killer. The OOM Killer has the grisly job of picking which process needs to die in order to get your system back to a manageable state. It actually follows a rather precise formula, endeavoring to make a decision which will be most convenient to the user and fair to the running processes.

The details are available here:

http://linux-mm.org/OOM_Killer

Gentoo Compiling Tip

| No Comments
And now, a stunning compiling tip from Captain Obvious:

If you are ever compiling the Linux kernel on any system with 500 Mhz of processing power or less, make sure to remove all the driver and filesystem options your don't need. As I'm finding out the hard way, it takes around 5+ minutes to compile the modules for each file system on a computer this slow. I feel especially silly because I am only actually using one disk file system on the machine (xfs).

Continous Monitoring of a Text File

| No Comments
Probably should have known this before now, but in any case: I found out that you can continuously monitor the output of a file using the "tail" command. In other words, if you use the command

tail -f <filename>
the program will output the last 10 lines of the file, and then it will continue output additional data as the file grows.  This is really handy, I found, when I needed to watch the output of genkernel.log while I was compiling my gentoo kernel.
The site blog for indicium.us.
Linux Projects
Online Games
Unsung Linux Games

RSS Feed

Powered by Movable Type 4.21-en
and GNU/Linux


Creative Commons License
The content of this blog is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. See my copyleft page for more details.