Skip to content

Commit

Permalink
修复HDFS上的readBytesFromOtherInputStream:#536 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed May 21, 2017
1 parent 09b9a8f commit e6f0617
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/com/hankcs/hanlp/corpus/io/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,27 @@ private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws I
}

/**
* 将InputStream中的数据读入到字节数组中
* 将非FileInputStream的某InputStream中的全部数据读入到字节数组中
*
* @param is
* @return
* @throws IOException
*/
public static byte[] readBytesFromOtherInputStream(InputStream is) throws IOException
{
byte[] targetArray = new byte[is.available()];
readBytesFromOtherInputStream(is, targetArray);
is.close();
return targetArray;
ByteArrayOutputStream data = new ByteArrayOutputStream();

int readBytes;
byte[] buffer = new byte[Math.max(is.available(), 4096)]; // 最低4KB的缓冲区

while ((readBytes = is.read(buffer, 0, buffer.length)) != -1)
{
data.write(buffer, 0, readBytes);
}

data.flush();

return data.toByteArray();
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/hankcs/hanlp/corpus/io/IOUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hankcs.hanlp.corpus.io;

import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.util.Random;

public class IOUtilTest extends TestCase
{
public void testReadBytesFromOtherInputStream() throws Exception
{
Random random = new Random(System.currentTimeMillis());
byte[] originalData = new byte[1024 * 1024]; // 1MB
random.nextBytes(originalData);
ByteArrayInputStream is = new ByteArrayInputStream(originalData){
@Override
public synchronized int available()
{
int realAvailable = super.available();
if (realAvailable > 0)
{
return 2048; // 模拟某些网络InputStream
}
return realAvailable;
}
};
byte[] readData = IOUtil.readBytesFromOtherInputStream(is);
assertEquals(originalData.length, readData.length);
for (int i = 0; i < originalData.length; i++)
{
assertEquals(originalData[i], readData[i]);
}
}
}

0 comments on commit e6f0617

Please sign in to comment.