Two Laptop Bag*
The moose likes Beginning Java and the fly likes Byte streams and the super keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte streams and the super keyword" Watch "Byte streams and the super keyword" New topic
Author

Byte streams and the super keyword

Olakunle Oladipo Oni
Greenhorn

Joined: Aug 28, 2008
Posts: 18
(1) I wrote a java program involving the use of the FileInputStream class to read from a plain text and executable file. As expected, being a byte stream, the
generated result was a series of integers representing the individual bytes read from the stream. The sample results look like this:

49 43 182 0 23 54 4 178 0 20 187 0 11 89 21 4 184 0 26 183 0 16 18
......................and so on. The integers spans rows. I use my program to determine the byte count. Of course i know if I'm to read the plain text file and
view it as it really is i.e. see the text in the result i should use a FileReader character stream. My concern for this question is not character streams. I'm
interested in knowing how the individual bytes got resolved into integers of varying ranges. I'm aware that byte data type have a range of 0-255 for unsigned
integers and that's why non of the output integers exceed that range safe for the -1 integer value that marks the end of the file. Most interestingly, the numbers
of the integers exceeds the number of words making up the plain text. How did the integers of varying ranges came to be? Are the individual integers a result of
each individual character of the text. Is there a provision for the spacing between the words that make up the text. What are the zeros in the outcome
representing? I'll appreciate a clear explanation.

(2) One of the established rules of implementing proper object formation via inheritance is to make a call to the superclass constructor the first statement
after defining the subclass constructor. Something i want to get clearly is that, if the superclass portion of my subclass object get initialized via call to
super(a,b); does this actually translate to the automatic initialization for this instance variables for the subclass portion of the code even if this variable
have private access in the superclass? If the subclass constructor is used as a means of initializing the superclass portion of the code, is the subclass
constructor actually initializing itself with these values as well since it has access to the variables by inheritance? Is it typical that the superclass and subclass
constructors can intitialize themselves differently (with different values) for the same kinds of attributes(variables)? I'll appreciate a clear answer to this
question putting into consideration the access level from the superclass.

Olakunle Oni

.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Olakunle Oladipo Oni wrote:(1) I wrote a java program involving the use of the FileInputStream class to read from a plain text and executable file. As expected, being a byte stream, the
generated result was a series of integers representing the individual bytes read from the stream. The sample results look like this:

49 43 182 0 23 54 4 178 0 20 187 0 11 89 21 4 184 0 26 183 0 16 18
......................and so on. The integers spans rows. I use my program to determine the byte count. Of course i know if I'm to read the plain text file and
view it as it really is i.e. see the text in the result i should use a FileReader character stream. My concern for this question is not character streams. I'm
interested in knowing how the individual bytes got resolved into integers of varying ranges. I'm aware that byte data type have a range of 0-255 for unsigned
integers and that's why non of the output integers exceed that range safe for the -1 integer value that marks the end of the file. Most interestingly, the numbers
of the integers exceeds the number of words making up the plain text. How did the integers of varying ranges came to be? Are the individual integers a result of
each individual character of the text. Is there a provision for the spacing between the words that make up the text. What are the zeros in the outcome
representing? I'll appreciate a clear explanation.
.


Most text files use ASCII as the mapping -- meaning it uses byte values up to 127 to represent characters. Google for ASCII table to see the mapping.

Other text files uses unicode, which is actually ASCII -- extended with character that takes more than one byte.


Regardless of the mapping, the characters, all of the them, including the spaces, tabs, carriage returns, etc. maps to byte representations.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Olakunle Oladipo Oni wrote:
(2) One of the established rules of implementing proper object formation via inheritance is to make a call to the superclass constructor the first statement
after defining the subclass constructor. Something i want to get clearly is that, if the superclass portion of my subclass object get initialized via call to
super(a,b); does this actually translate to the automatic initialization for this instance variables for the subclass portion of the code even if this variable
have private access in the superclass? If the subclass constructor is used as a means of initializing the superclass portion of the code, is the subclass
constructor actually initializing itself with these values as well since it has access to the variables by inheritance? Is it typical that the superclass and subclass
constructors can intitialize themselves differently (with different values) for the same kinds of attributes(variables)? I'll appreciate a clear answer to this
question putting into consideration the access level from the superclass.
.


Not exactly sure what you are asking... so will take a guess.

Keep in mind that a subclass extends a superclass -- so it has all the components of the superclass, which has to be initialized. This initialization is done with the constructors of the superclass.

Henry
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2850
    
  11

There are a number of ways to encode text files, and if you use a FileReader to read one, you have to tell it which encoding to use. In an ASCII encoding, for example, each character is represented as a one byte integer. White space characters such as the single space, tab, carriage return, and line feed each have their own integer encoding just like letters, numbers, and symbols.

The limitation of ASCII is it can't represent all the world's alphabets. Other encodings are used for that, particularly UTF-8. In UTF-8, a single character may be encoded as one to three bytes, and that may be why you see 0s in your output. That 0 byte might be just one byte of a multibyte encoding.

Note that the range of a Java byte is -128 to 127, not 0 to 255 as you say. However, a byte is just an arrangement of bits. The same arrangement can be considered -1 or 255, depending how you look at it, but Java will look at it as -1 for arithmetic, comparison, or printing. You see values above 127 in your output, which means you aren't just printing bytes through System.out.print().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte streams and the super keyword