我必须部分地读取资源,然后稍后(在非常不同的时间点),我需要跳过我最初读取的字节数。我需要粘上我读过的两个部分。
这是我的代码的一个非常简单的说明。我正在使用单独的ByteArrayInputStream
- s,因为如上所述,调用将完全不相互关联(几乎可以肯定,不使用相同的InputStream
)。
我不太确定这里出了什么问题。我没有获得值String
的连续This is a big fat super long text has no meaning, but is good for the test.
,而是获得:This is a big fat super long text has no meaning, but is good for the test.aning, but is good fo
。
public void testFoo()
{
String s = "This is a big fat super long text has no meaning, but is good for the test.";
ByteArrayInputStream bais1 = new ByteArrayInputStream(s.getBytes());
ByteArrayInputStream bais2 = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
int size = 32;
byte[] bytes = new byte[size];
int total = 0;
int len;
while ((len = bais1.read(bytes, 0, size)) != -1)
{
baos1.write(bytes);
baos1.flush();
total += len;
if (total >= size)
{
// This is here just to illustrate that
// we are reading a few bytes, and then
// just terminating before the rest of
// the stream has been read.
break;
}
}
bytes = new byte[size];
bais1.close();
System.out.println("Read " + total + " bytes.");
// Here we are supposed to skip the number
// of bytes that have already been read:
bais2.skip(total);
System.out.println("Skipped " + total + "/" + s.getBytes().length + " bytes.");
while ((len = bais2.read(bytes, 0, size)) != -1)
{
baos2.write(bytes);
baos2.flush();
total += len;
}
System.out.println("Original: " + s);
System.out.println("Partial read1: " + new String(baos1.toByteArray()));
System.out.println("Partial read2: " + new String(baos2.toByteArray()));
System.out.println("Read " + total + " bytes.");
}
有人可以指出错误是什么以及如何解决?我不太明白为什么在第二次读取过程中跳过字节数后,读取数据的结尾会搞砸。请指教!
答案 0 :(得分:3)
在第二个读取循环中,您忽略了读取的字节数,因此缓冲区中已经存在的字节将被附加到输出流中。
尝试将其更改为:
$scope.searchForUser = function() {
$scope.selectedUser = null;
$scope.selectedRepo = null;
$scope.returnedRepos = [];
$scope.returnedCommits = [];
var url = "https://api.github.com/search/users?q=" + $scope.user;
if($scope.user.length === 0){
$scope.returnedUsers = null;
return;
}
function updateUsers() {
$http.get(url).
success(function(data,status) {
if(status===200) {
$scope.returnedUsers = data.items;
}
}).
error(function(data,status){
alert("something happened with quhhnnhhhh");
});
}
if($scope.userSearchTextTimeout)
{
$timeout.cancel($scope.userSearchTextTimeout);
}
$scope.userSearchTextTimeout = $timeout(function()
{
$scope.user = $scope.user;
}, 500);
};