0

I'm copying the code and the output as well I want to know why is it taking so much time to execute the program when provide larger data file as input.

Please see through out the code and give me suggestions and try to execute.

java Program:

FileInputStream fis = new FileInputStream("rose.jpg"); //use your own binary file name
DataInputStream dis = new DataInputStream(fis);
try {
    byte buffer[] = new byte[16];

    while((line = dis.read(buffer)) != -1)
    {
        for(int i = 0; i < line; i++)
        {

                value = Integer.toHexString(0xFF & buffer[i] | 0x100).substring(1);
                nextvalue = nextvalue+""+value;

        }
            if(a == 0)
            {
                incValue = nextvalue.substring(0, 32);
                System.out.println(incValue);
            }
            else
            {
                counter = counter + 32;
                incValue = nextvalue.substring(counter, counter + 32);
                System.out.println(incValue);
            }
            a++;

output:

ffd8ffe000104a464946000101020025
00250000ffdb00430002010101010102
01010102020202020403020202020504
04030406050606060506060607090806
0709070606080b08090a0a0a0a0a0608
0b0c0b0a0c090a0a0affdb0043010202
02020202050303050a0706070a0a0a0a
0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a

each line of the output will have 16 bytes of values.

Please, help me with this program what I have to modify and change so that program executes quickly.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
user3790765
  • 1
  • 1
  • 2
  • This is an excellent problem with which to learn several programming techniques. Don't be afraid to experiment and to try to improve on what you have after you first get it working. For an added challenge, print the equivalent ASCII characters in a 3rd column to the right, using `.` or some such to "stand in" for unprintable characters. You'll end up with a handy tool. – Hot Licks Jul 15 '14 at 15:33

3 Answers3

1

Two things you should do: 1) Wrap your file input stream in a BufferedInputStream.

InputStream bis = new BufferedInputStream( fileInputStream, 1024*1024 );

This will reduce the number of times you read from storage. 2) Buffer the output by appending to a StringBuffer instead of writing directly to the output. Note that if your file is very large you may need to write the stringbuffer and reset it periodically... depends on how large the files are of course.

Pat Niemeyer
  • 5,930
  • 1
  • 31
  • 35
  • 1MB buffer size is totally excessive and will not improve performance notably over the default size of 8K. – Durandal Jul 15 '14 at 17:38
  • @Durandal It totally depends on big the file is, how fast the machine is vs. storage, the latency, etc. He should experiment to find a good size for his situation. – Pat Niemeyer Jul 15 '14 at 18:40
  • while((line = bis.read(buffer)) != -1) { for(int i = 0; i < line; i++) { value = Integer.toHexString(0xFF & buffer[i] | 0x100).substring(1); sb.append(value); } The output is getting throwing exception at the end like 4775a61e52c3129c4aba3af1f28c8ee0 9050718e15a8189321d626399ab2612f 212f89f4f9ff0015d03b625cfb990c8a 1c36dc8c13e636f4e74b6df4af853207 49ea39e78c727df55b6f0d5bc90a54fd f7aba3f8f258496d0256400474236335 Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 216896 – user3790765 Jul 16 '14 at 13:11
0

Use a new BufferedInputStream(new FileInputStream(...)).

Do not use a String nextValue but a StringBuilder. String + slows down tremendously.

You might use

String.format("%02x", buffer[i] & 0xFF);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • A better approach might be to create a single Formatter using the zero-argument constructor, as it will create a StringBuilder internally. `String.format` would create a new Formatter each time it is called. – VGR Jul 15 '14 at 16:56
  • I need to fetch the binary data from a file and print the data 16 bytes line by line is it possible by using append method or any other suggestions. – user3790765 Jul 15 '14 at 17:40
  • The main speed issue lays with `String +, +=` IMO: do away with `nextValue` (PrintWriter, or at least StringBuilder). One could use `"%02x%02x%02x...", ints[0], ints[1], ... `, but @VGR's idea is more straightforward. – Joop Eggen Jul 17 '14 at 06:26
0

You make nextvalue bigger and bigger and bigger. The substring operations on it become expensive. substring is an O(n) operation.

instead of

            if(a == 0)
            {
                incValue = nextvalue.substring(0, 32);
                System.out.println(incValue);
            }
            else
            {
                counter = counter + 32;
                incValue = nextvalue.substring(counter, counter + 32);
                System.out.println(incValue);
            }
            a++;

why not just:

System.out.println(nextvalue);
nextvalue = "";
Community
  • 1
  • 1
Cruncher
  • 7,641
  • 1
  • 31
  • 65