DataInputStream class design error source

2010-05-08  来源:本站原创  分类:Java  人气:174 

I am a Java beginner, please predecessors Xiangjie.
Yesterday, when this part of learning Java stream, see jdk source code, personal feel sun in the design DataOutputStream this class is not appropriate or even a BUG.
DataInputStream in a method readInt (), source code is
public final int readInt () throws IOException (
int ch1 = in.read ();
int ch2 = in.read ();
int ch3 = in.read ();
int ch4 = in.read ();
if ((ch1 | ch2 | ch3 | ch4) <0)
throw new EOFException ();
return ((ch1 <<24) + (ch2 <<16) + (ch3 <<8) + (ch4 <<0));

)

Where ch1, ch2, ch3, ch4 may be negative, as it will throw an exception.

False entry into the data as a positive integer, then ch2 <<16, ch3 <<8, ch4 <<0 may be negative, so there will be added to the data read out is not the proper context.

This is my validation code shift operation

byte b = (-1);

System.out.println (b);

int i = b;

i = i <<8;

System.out.println (i);

Output: -1 and -256

The method in DataOutputStream writerInt (), source code is

public final void writeInt (int v) throws IOException (

out.write ((v>>> 24) & 0xFF);

out.write ((v>>> 16) & 0xFF);

out.write ((v>>> 8) & 0xFF);

out.write ((v>>> 0) & 0xFF);

incCount (4);

)

Comparison between the two is puzzling me, so I personally venture that is JDK source code errors.

Personal proposed readInt () code should be amended to read as follows code

public final int readInt () throws IOException (

byte [] b = new byte [4];

for (int i = 0; i <4; i + +) (

b [i] = in.read ();

)
return new BigInteger (b). intValue ();

)

In addition DataInputStream in readLong (), readShort () so the code has this error. Please predecessors Xiang Jie, for a beginner in this area is very confused.

If I suggest please email me, my email is: [email protected]

Please forgive my predecessors here venture to obscurity, Younger here thank you.

相关文章
  • DataInputStream class design error source 2010-05-08

    I am a Java beginner, please predecessors Xiangjie. Yesterday, when this part of learning Java stream, see jdk source code, personal feel sun in the design DataOutputStream this class is not appropriate or even a BUG. DataInputStream in a method read

  • AS3 difficult to check the strength of the error source of reference at work 2010-04-08

    In the January 24, 2010 held in Shanghai exchange in the Flash developer, I spoke at the venue Tween, URLLoader, and the like, will use weak references as a result of the error listener, why go wrong? The following is wrong about the root causes can

  • Android transplantation: wifi design principles (source analysis) 2010-08-08

    Initialization in SystemServer startup, will generate a ConnectivityService example of try ( Log.i (TAG, "Starting Connectivity Service."); ServiceManager.addService (Context.CONNECTIVITY_SERVICE, new ConnectivityService (context)); ) Catch (Thr

  • Top 40 web design psd source file to download material 2010-11-22

    From http://www.iguoguo.net/html/y2010/5806.html This is from 365PSD turn, click on the picture will jump to the download areas, 365PSD is a good site, and most recent person this time slowly increased, by some design are recognized. Interested frien

  • To "address only to find the source code through the collapse of the error line of" A supplement to and improvement of 2010-04-25

    Download the source code for reading old Luo's "only through the collapse of the addresses to find the source code of the error line" (the "Roman") article, the feeling that the text can still learn a lot of things. However, there are

  • Software architecture design points 2010-03-26

    Today's software industry, software architecture decisions, and the software is good or bad. Other than people like skeleton, skeleton, etc. If people are not good, then people are very vulnerable. Software architecture in software engineering that w

  • Outline design specification 2010-03-22

    Outline design specification 1. Introduction 1. The preparation of purpose Formally entered the stage of development from the actual software development phase, this phase is completed the general design of the system and clear the system data struct

  • The preparation of outline design specification document 2010-03-22

    The preparation of outline design specification document Reprinted from: wader Original Source: http://blog.51ww.com/wader/archive/2005/08/10/1932.51ww Make software to a certain level, and should take into account the design, design for a long time,

  • Software development design documentation 2010-12-20

    [Project Name] Outline design specification [V1.0 (version)] Prepared by ______________________ Reviewer ______________________ Approved by ______________________ [August 20, 1999] Outline design specification 1. Introduction 1.1 The purpose of writi

  • Offic not find a reason for the data source 2011-04-14

    Offic edited in the designer, if the data set does not specify the data source name, then the designer will default to the current database to design the report. However, if the report is deployed in the application server if there are multiple data

  • Share the source code is designed to 2007-02-26

    Source code is the design Jack Reeves of Deng Hui translation This article is taken, "Agile Software Development: Principles, Patterns, and Practice" (Tsinghua University Press, September 2003 edition). This is a great paper, the article written

  • (R) linux source code analysis tools, and read more 2010-11-01

    Reprinted from: http://hi.baidu.com/likeadream/blog/item/7664bb349daf0049241f144c.html Reading tool for Windows source code Souce Insight With its ease of use and a variety of programming languages, no doubt in this area, "lead Big Brother." Lin

  • RAQ Report data source can not find a reason 2011-04-14

    RAQ Report edited in the designer, if the dataset does not specify the data source name, then the designer will use the current default database to design the report. However, if the report is deployed application server there are multiple data sourc

  • How a failed system is generated: design deficiencies 2010-08-19

    Not long ago, a friend provides a system case S, a rough analysis of its architecture, and visit most of the code. From a commercial point of view, S may be successful, but from a technical point of view, the system obviously has a lot of problems. I

  • Database design using powerdesigner 2011-03-17

    Database design using powerdesigner really easy, here are some commonly used settings Additional: Toolbar disappeared Palette (Palette) Shortcut Bar disappeared PowerDesigner shortcut toolbar palette disappeared, how to re-open, looking back it Inter

  • powerDesigner use (construction of the index since added Check design models) --- turn 2011-06-04

    Model checking in the Existence of reference problem in the tool-> check model Select which option tab, then select the table, the next thing you start to understand Not duplicate the name, but by default you must create an index for each table / ass

  • Overview Summary of the design of how to do that - the design of structured methods and object-oriented design methods 2009-07-02

    Overview Summary of the design of how to do that - the design of structured methods and object-oriented design methods At 23:52 on February 12, 2008 Overview Summary of the design of how to do that - the design of structured methods and object-orient

  • 2008 50 Best CSS Design 2009-08-24

    This is webdesignerwall.com 2008 selected 50 best CSS design, material source Best Web Gallery . From this year's design trend, more and more people are using the background, as well as JavaScript frameworks (jQuery and MooTools) to enhance the user

  • WEB design experience - from the design of competent microsoft.com 2009-09-27

    WEB design experience - from the design of competent microsoft.com As a design director, Peter Stern has led microsoft.com redesigned the home page and the development of five different interactive tools, these tools are used to download center, cata

  • Search Engine Optimization Design Guide 2009-11-09

    Nearly 10% of people do not think the seo (search engine optimization) is the designer should shoulder responsibility. I am even more surprising is that even 24% do not know what is seo. If you do not know what is seo help seo, or do not understand w