Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading incorrect values?

Posted on 2013-05-28
5
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

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