1

我有跟随类,扩展线程。这个想法是在线程中提取日期,一切顺利,直到接收到的数据大于几千字节,然后我开始读取完整的不正确数据。

public class ThreadBooksPositions extends Thread
{

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

    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();

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

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

                    out.flush();
                }
                catch ( IOException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //you can configure the buffer size

                byte[] bO = out.toByteArray();

                if( out != null )
                {
                    try
                    {
                        out.close();
                    }
                    catch ( IOException e )
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                ByteBuffer data = ByteBuffer.wrap( bO );

                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();
            }
        }
    }

}
  • 什么可能导致读取不正确的数据?
4

1 回答 1

1

发现问题。似乎.available()导致了这个问题,尤其是在线程中。

于 2013-05-28T12:08:42.317 回答