Recent Post

Categories

Archives

Cow Computing

09 Sep 28

Some Useful Linux Commands to Network Info

Here, i would like to share with you all some commands that is useful in extracting network information from the OS.

1. Extracting IP of the current machine

ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'

2. Getting the Gateway of the current machine

route -n | grep 'UG[ \t]' | awk '{print $2}'

3. Updating the network settings

// ifconfig <network interface> <ip> netmask <subnetmask> broadcast <broadcast address>
ifconfig eth0 192.168.0.10 netmask 255.255.255.0 broadcast 192.168.0.255

4. Updating Gateway

// in order to achieve this, we need to first delete the
// existing gateway entry and then add the new one
route del default

// route add -net default gw <gateway address> netmask 0.0.0.0 dev eth0;
route add -net default gw 192.168.0.255 netmask 0.0.0.0 dev eth0

09 Sep 23

Java – Cloning an InputStream

There’s a moment when you have been given an InputStream, with its markSupported returned “False”. This means you could only read the stream one and only once, as you can’t call “reset” on it. So, what to do if you want to read the stream for more than one time and be able to re-read the previously read data. The answer is “CLONE”. My very own approach would be to use Apache Commons to turn the stream into string in which it acts as a base. Whenever i need that InputStream, i turns the string back into an stream without affecting the read position and data lost. OK, here’s the steps.

Suppose you got a request object with InputStream.

// request is the Request Object
// Turns InputStream into string
String base = IOUtils.toString(request.getInputStream());

// Whenever you need the InputStream
// Turn the string back to InputStream this way
InputStream in = new ByteArrayInputStream(base.getBytes());

09 Sep 8

Combine All DLL and EXE into One

Often when we are developing on the .NET platform, we might try to link some external library or even create our very own to facilitate the development. This usually results in a bunch of dll together with the exe sitting inside the bin folder. It’s ugly and troublesome for user in case certain dll is missing.

Now, we got a solution from a microsoft researcher. ILMerge is a utility developed to merge multiple dll/exe into one.

// To merge into a single exe.
ILMerge.exe /out:Combined.exe path_to_bin/original.exe path_to_lib/lib.dll

// To merge into a single dll
ILMerge.exe /out:Combined.dll path_to_lib/lib.dll path_to_lib/lib2.dll

*note that, the merge only work good with dll created under .NET platform.

09 Sep 7

.NET code to interact with Java’s Web Service

Recently, i am working on a library to allow .NET developer to interact with a web service built on Java. It’s a SwA (Soap with Attachment). The reason for me to do this is because there’s been no support for such operation in .NET framework and users who want to deal with such scenario will have to write their own code bit by bit to accomplish the job, which is TEDIOUS…

The library i built relies on an open source library called PocketSoap. Its support on Attachment and its ease of use is appealing and i at last chosen to give it a try and build a POC on top of it. Below is a simple usage sample for the PocketSoap library.

using System;
using PocketSOAP;
using PocketSOAPAttachments;

namespace PSTryout
{
    public class psWS
    {
        CoEnvelope e = new CoEnvelopeClass();
        e.Body.Create("id", 1, "", null, null);
        e.EncodingStyle = "";

        HTTPTransport h = new HTTPTransport();
        h.SOAPAction = "query";
        h.Timeout = 30000;
        h.Send("http://localhost:8080/ws/", e.Serialize());

        string enc = "";
        e.Parse(h, enc);
    }
}

09 Sep 6

JAVA Substring Bug!

Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring returns the part of the original string rather than a new one. Thus, GC wont collect the original string. So when the file or string get larger and larger, it’s actually taking up more and more memory, and thus OutOfMemory Error. To solve this, there’s nice workaround which is simple.

String newString = new String( oldString.subString(0, 5) );

Simple, Right?!