Results 1 to 13 of 13
  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default FileOutputStream to GIF

    Hey there
    I am confused, and have a lack of understanding about what FileOutputStream actually does.
    Heres my code I got from a book:
    Java Code:
    package test;
    import java.io.*;
    
    public class ByteWriter {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int[] data = {71, 73, 70, 56, 57, 97, 13, 0, 12, 0, 145, 0,
    	            0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 44, 0,
    	            0, 0, 0, 13, 0, 12, 0, 0, 2, 38, 132, 45, 121, 11, 25,
    	            175, 150, 120, 20, 162, 132, 51, 110, 106, 239, 22, 8,
    	            160, 56, 137, 96, 72, 77, 33, 130, 86, 37, 219, 182, 230,
    	            137, 89, 82, 181, 50, 220, 103, 20, 0, 59};
    		try{
    			FileOutputStream file = new FileOutputStream("pic.gif");
    			for(int i = 0; i<data.length; i++)
    				file.write(data[i]);
    			file.close();
    		}catch(IOException e){
    			System.out.println("Error -- " + e.toString());
    		}
    	}
    
    }
    I then open "pic.gif" in notepad and it gives me:
    Java Code:
    GIF89a
               ,    
       &-yx3nj*8`HM!V%۶YR2g ;
    How did the array get "translated" to the content inside the gif?
    Sorry if this is a stupid question
    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dewitrydan View Post
    I then open "pic.gif" in notepad and it gives me:
    Java Code:
    GIF89a
               ,    
       &-yx3nj*8`HM!V%۶YR2g ;
    How did the array get "translated" to the content inside the gif?
    Sorry if this is a stupid question
    It isn't; you didn't write a text file and notepad can only properly show text files. A quick check shows that you have correctly written all the bytes in the original array. For a more advanced class have a look at the ImageIO class; it can read and write pictures (gif, jpeg, png etc) in several formats.

    kind regards,

    Jos

  3. #3
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Thanks for the reply.
    I want to know what the method "write" actually does.
    What does it actually do to the input data from the array exactly?
    thanks

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What does it actually do to the input data from the array exactly?

    The for loop works along the array and, for each int value it finds, the write() method "Writes the specified byte to this file output stream. (Implements the write method of OutputStream)", that is, it "Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."

    As always a description of what a method does is to be found in its API documentation.

    Saying that each byte is written to the output stream (the file) is about all you can say, along with the observation that the bytes will be exactly the same as those you later obtain by reading them from the file using a FileInputStream.

  5. #5
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Thanks for the quick reply.
    I don't quite get what is meant by the terms "eight low-order bits" and "high-order bits".
    Does the write method translate the input to the binary code equivalent? or something?
    Thanks for your time.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I don't quite get what is meant by the terms "eight low-order bits" and "high-order bits".
    Does the write method translate the input to the binary code equivalent?

    Let's look at the very first int in the array: 72.

    Now, strictly speaking, 72 is just a number. Various representations of it may involve bits or have a base like binary or decimal. But the number is just a number: six dozen in this case.

    However it's convenient to think of the number in its binary form:

    Java Code:
    00000000000000000000000001001000

    I could have written it in binary as 1001000 but I put all those zeros on the left because Java ints can typically get that big. The left hand end of the number is called the "high order bits" and the right hand end the "low order bits". I hope the reason is obvious. (say if not)

    Now the documentation is saying that when write() comes to write an int only the 8 low order (right hand) bits are written. That is only 01001000. The other 24 bits are thrown away. Consequently the following ints (whatever they are!) will all be written the same as 72:

    Java Code:
    00000000000000000000000001001000
    01010101010101010101010101001000
    11111111111111111111111101001000

    If you think about it for a while you will see that there are exactly 256 different values that can be written this way: from 00000000 (== zero) to 11111111 (== CCLV) which are referred to as bytes. (I write them with words or roman numerals to stress the fact that as numeric quantities they have neither bits nor base - important as bits might be to the folk who make disk drives)

    You might be wondering why the write method doesn't take a byte as an argument rather than an int if it only going to ignore those 24 bits on the left. I would guess this is related to the fact that Java's language designers decided to use "byte" for a different set of 256 values: those from -128 to 127.
    Last edited by pbrockway2; 10-09-2010 at 10:01 AM.

  7. #7
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Thank you
    That cleared allot up.
    So just to clarify
    I ran this bit of code:
    Java Code:
    package test;
    import java.io.*;
    
    public class ByteWriter {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int[] data = {72};
    		try{
    			FileOutputStream file = new FileOutputStream("pic.gif");
    			for(int i = 0; i<data.length; i++)
    				file.write(data[i]);
    			file.close();
    		}catch(IOException e){
    			System.out.println("Error -- " + e.toString());
    		}
    	}
    
    }
    I opened pic.gif in notepad, and it gave me:
    Java Code:
    H
    So the write method converted 72 in to the Ascii character equivalent?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dewitrydan View Post
    So the write method converted 72 in to the Ascii character equivalent?
    No conversion was needed: the character H is represented by the byte value 72. It's notepad that interprets the code 72 as a capital H.

    kind regards,

    Jos

  9. #9
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    oh ok thank you for your time mate

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    why are you using notepad to open a .gif file ???

  11. #11
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    why are you using notepad to open a .gif file ???
    To see exactly what is in the file. As I did not know exactly what the "file.write" function did to the data inputted from the array.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    To see exactly what is in the file
    Notepad only can show character data. The binary bytes that can be in gif files will show as ? or as small boxes. You need to use a hex editor to look at binary files like gifs.

  13. #13
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Notepad only can show character data. The binary bytes that can be in gif files will show as ? or as small boxes. You need to use a hex editor to look at binary files like gifs.
    Oh ok thank you

Similar Threads

  1. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 06:36 PM
  2. FileOutputStream > int > FileInputStream
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-11-2009, 05:03 PM
  3. Replies: 6
    Last Post: 09-14-2009, 11:46 PM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 10:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •