Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Reading incorrect values?

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
YordanYanakiev
Asked:
YordanYanakiev
  • 3
  • 2
2 Solutions
 
CEHJCommented:
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
 
YordanYanakievAuthor Commented:
Is this the issue ?
What can I do to make it right ?
0
 
CEHJCommented:
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
 
YordanYanakievAuthor Commented:
Thank You! This finally worked :)
0
 
CEHJCommented:
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now