Solved

Reading incorrect values?

Posted on 2013-05-28
5
300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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

19 Experts available now in Live!

Get 1:1 Help Now