Solved

Reading incorrect values?

Posted on 2013-05-28
5
321 Views
Last Modified: 2013-05-28
I have the follow class, extending Thread. The idea is to extract the date inside the thread, and everything goes well, until the sometime when the received data is larger than a few kilobytes, and then i am starting to reading complete incorrect data. The first few hundreds of readings for lat and lng is correct, but next ones is completely incorrect.
public class ThreadBooksPositions extends Thread
{

    public ThreadBooksPositions()
    {
    }
    ..
    // default constructors

public byte[] inputStreamToByteArray( InputStream iS )
{
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    byte[] bf;
    try
    {
        bf = new byte[ iS.available() ];

        while ( iS.read( bf ) != -1) 
            out.write( bf ); //copy streams

        out.flush();
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    }

    byte[] bO = out.toByteArray();

    if( out != null )
    {
        try
        {
            out.close();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    return bO; 
}

public ByteBuffer inputStreamToByteBuffer( InputStream iS )
{
    return ByteBuffer.wrap( inputStreamToByteArray( iS ) );
}

    public void run()
    {
        InputStream       iSS           = null;
        HttpURLConnection connection    = null;
        Integer sectionsDescriptorSize1 = 0;
        Integer sectionsDescriptorSize2 = 0;

        try
        {

            URL url = new URL( "192.168.1.4/bookstore.asp?getbooks" );
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod( "GET" );
            connection.connect();

            iSS = connection.getInputStream();

            BufferedInputStream bIS = new BufferedInputStream( iSS );

            if( bIS.available() > 4 )
            {
                Float   lat           = 0F;
                Float   lng           = 0F;

                ByteArrayOutputStream out = new ByteArrayOutputStream();

                ByteBuffer data = inputStreamToByteBuffer( bIS );

                sectionsDescriptorSize1 = data.getInt();
                sectionsDescriptorSize2 = data.getInt();

                ByteBuffer sectionData;

                try
                {
                    if( sectionsDescriptorSize1 > 0 )
                    {
                        byte[] bAS0 = new byte[ sectionsDescriptorSize1 ];
                        data.get( bAS0 );
                    }

                    if( sectionsDescriptorSize2 > 1 )
                    {
                        // trajectory
                        byte[] bAS1 = new byte[ sectionsDescriptorSize2 ];
                        data.get( bAS1, 0, sectionsDescriptorSize2 );

                        sectionData = ByteBuffer.wrap( bAS1 );

                        Boolean readingFailed = true;

                        if( sectionData != null )
                        {
                            while( sectionData.available() > 1 )
                            {
                                try
                                {
                                    readingFailed = false;
                                    lat           = sectionData.getFloat();      // 4
                                    lng           = sectionData.getFloat();      // 4

                                }
                                catch( Exception e )
                                {
                                    readingFailed = true;
                                }

                                try
                                {
                                    if( readingFailed == false )
                                    {
                                        addBookStorePosition( lat, lng );
                                    }
                                }
                                catch (Exception e) 
                                {
                                }
                            }
                        }

                }
                catch( Error e )
                {
                }
            }
        }
        catch( IOException e )
        {

        }
        finally
        {
            if( iSS != null )
            {
                try
                {
                    iSS.close();
                }
                catch( IOException e )
                {

                }           
            }

            if( connection != null )
            {
                connection.disconnect();
            }
        }
    }

}

Open in new window


- What cause reading of incorrect data, and how to fix it ?
0
Comment
Question by:YordanYanakiev
  • 3
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 39200931
You can't use available like that. Read the api docs carefully and you'll see (and hopefully remember) why. See http://technojeeves.com/joomla/index.php/free/51-copying-streams
0
 
LVL 1

Author Comment

by:YordanYanakiev
ID: 39200938
Is this the issue ?
What can I do to make it right ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39200949
Use the code at the link i gave you? (It contains a method called ... inputStreamToByteArray)
URL url = new URL( "192.168.1.4/bookstore.asp?getbooks" );

Open in new window

If that ASP script sets the Content-Length header, you can use that to size the byte array. Otherwise, you have no other accurate way of knowing the size.
0
 
LVL 1

Author Closing Comment

by:YordanYanakiev
ID: 39200983
Thank You! This finally worked :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39201012
:)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now