Snippet IT IT News, Programming, Internet and Blogging

12Jan/100

Java: Use ByteBuffer As InputStream

Sometime, your program may need to read data from a ByteBuffer buffer into a InputStream object. There is no class in Java library that provide the facility to do the conversion.

Anyway, to use a ByteBuffer object as an InputStream is pretty simple. What you need to do is to write a class wrapper that inherit InputStream and override the read() function in InputStream.

12Jan/100

100th Posts For Snippet IT

Finally, this blog has reached 100 posts! Yeah!

It really takes a long time for me to do this because I'm not blogging this site full timely. Second, sometime I'm just out of idea what to blog.

I will try work harder to share more information on programming and anything related to information technology in this new year. Thanks for reading!

5Jan/102

Java: Format Integer Into Fixed Width String

Sometime, you may want to format an integer or a long value into a fixed width String. You need to display the value in fixed width especially in reports where you want to keep the numbers in aligned (e.g. 3456 as 0003456 and 1234 as 0001234).

Other than that, you may also want to format the integer value for a better visibility. For example, to display integer value as a document number (e.g. 12345678 as 00-01234-5678), for example an account number.

The Java.format() is not flexible enough to solve my problem. At least it cannot be used to format a fixed length document number in the format I want: XX-XXXX-XXXX. Therefore I decide write my own format function.