Solved

Reading incorrect values?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 83
topping1 challenge 7 71
eclipse package explorer vs project explorer view 2 72
eclipse formatting 6 68
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

947 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

21 Experts available now in Live!

Get 1:1 Help Now