Tuesday, March 31, 2009

Java I/O API is very vast. I have shown how to use BufferedInputStream and BufferedOutput stream to read and write integers as bytes to a file.My test case only involves 5 integers but the program is meant for a large number of integers.

BufferedInputStream:My input stream is binary and not character. So I have used the stream type BufferedInputStream. I have wrapped the plain FieInputStream with the BufferedInputStream so that the input can be buffered. Then I have used the following method int read(byte[] b,int off,int len) to read up to len bytes of data from the stream into an array of bytes, thereby, improving performance.

BufferedOutputStream:I have used the BufferedOutputStream to write all the bytes at once instead of using the plain FileOutputStream.

/** * This method writes all the bytes to a file in a one shot(hopefully without blocking). * It is more efficient than writing 1 integer at a time in a file. * @param name filename * @param b Array of bytes to write */ public static void writeBytesToFile(String name, byte[] b) throws IOException{ BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(name)); bos.write(b); bos.flush(); } finally { bos.close(); } }

/** * This helper method actually converts each integer to 4 bytes. * See the next method for another way to do the same thing. * @param integer Integer to be converted to bytes * @param b The byte array into which the bytes have to be stored * @param offset The start offset in b */ public static void intToByte(int integer, byte[] b, int offset) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(integer); dos.flush(); System.arraycopy(bos.toByteArray(), 0, b, offset, numOfBytesInInt); }

/* Instead of using the intToByte method, one could also manually convert * the integer to an array of bytes using bit arithmetic. * Here is a way to do it. To use this method, just replace the call * to the method intToByte() with generateByte() above * * private static void generateByte(int integer, byte[] b, int offset) { b[offset]=(byte)((integer & 0xff000000)>>>24); b[offset+1]=(byte)((integer & 0x00ff0000)>>>16); b[offset+2]=(byte)((integer & 0x0000ff00)>>>8); b[offset+3]=(byte)((integer & 0x000000ff)); }*/

}

Test It

import java.io.*;import static java.lang.System.out;

public class TestingFiles {

private static final String filename = "INPUTBYTES";

/* * I am just testing with 5 integers but you can practically * use any number which is a multiple of 4. */ private static final int numOfBytes = 20;

No comments:

Post a Comment

About Me

I am a senior software engineer with interests in designing and developing mission-critical applications on Java platform. I love developing software applications and Java Technology. I am also deeply interested in topics such as cloud computing and Web 2.0 technologies. I will be using this blog to share my experience, thoughts and knowledge about matters related to software technology.