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.

5Jan/100

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.

17Dec/090

Java: Continuously Read Data From FileChannel Without MappedByteBuffer

When programmer writes a program to read data from a file continuously and sequentially with a fixed data type reading sequence (e.g. to read three integers then 2 doubles repeatedly), he or she may find the MappedByteBuffer from Java library is useful.

10Nov/090

Java: Loading Large Data into JTable or JList

In software programming (especially Graphical User Interface or GUI programming), many times, we need to load data from file or database into a table or list to be displayed to user on computer screen. Most of the time, the size of the data can be determined only during runtime. The data could be very small to a few rows of record and could be very large to a few millions rows of record.

18Aug/090

Java: Format Long Integer Into Hexadecimal String

In software programming, sometime you may want to convert a long integer or integer into hexadecimal string for viewing purpose or also as a text format to be stored in text file or database.

The Java API itself does not have a class or function to convert a long into a fixed 16 characters hexadecimal string or an integer into  fixed 8 characters hexadecimal string.

The Java's Long.toHexString(long i) or Integer.toHexString(int i) only able to convert the integer value to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0s and it does not support the reverse way (from hexadecimal string to long integer value).

5Jul/090

Java: Mortgage Payment Calculator

Mortgage Payment Calculator is a tool to calculate the monthly repayment for a fixed rate mortgage. The calculator is written in Java programming language (Java Applet) and can be ran at any web browser. You must have Java plug-in installed on your computer in order to run this web application.

16Jun/090

Java: Stop A Thread Correctly

In Java programming, it is very common to use threads to perform multi tasks simultaneously. Starting a thread is pretty easy but stopping a thread might be a headache for some programmers, especially when the thread needs to run a task that takes some times.

22May/090

Java: Format Bytes Array into Hexadecimal String

Sometimes, we may want to format a bytes array into hexadecimal (base-16) format for debugging purpose or for better readability. One byte has 8 bits and one byte can be represent by two hexadecimal characters (0 - 15 and A - F).

14May/090

Java: Least Recently Used (LRU) Cache

Least Recently Used or in short LRU is an performance optimizing algorithm. LRU algorithm is widely used in software programming as well as in hardware instructions (e.g. CPU).

In many case, in programming, we may want to cache some data from physical disk, for example database and files, but we cannot load all the data into memory due to the memory limitation on the server or PC. In those cases, you may want to consider to use LRU algorithm to solve the memory limitation problem.

11May/090

JSP: How to Declare Methods and Inner Class in JSP

JSP itself is just a plaint text and will be translated into Java source file (actually a servlet), compiled into class file and only interpreted at the end during runtime. All these happen at the backend.

When the whole JSP is translated into Java source file, all the HTML codes will be translated and source lines in <% %> tag will be included in a function of the translated servlet source file.