发布于2024-12-03 10:15 阅读(196) 评论(0) 点赞(4) 收藏(2)
我有以下类,扩展了 Thread。想法是在线程内提取日期,一切顺利,直到收到的数据大于几千字节时,我才开始读取完全错误的数据。
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();
}
}
}
}
找到问题了。似乎.available()导致了这个问题,尤其是在线程中。
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/694200/52b73ab5364e8f18fd86/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!