| | |
| | | byte[] buffer = new byte[2048]; |
| | | try { |
| | | int actRead = inputStream.read(buffer); |
| | | |
| | | detector.handleData(buffer, 0, actRead); |
| | | |
| | | //识别结束必须调用这个方法 |
| | | detector.dataEnd(); |
| | | detector.reset(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | } |
| | | return new ByteArrayInputStream(Files.newInputStream(tempFile).readAllBytes()); |
| | | } |
| | | |
| | | public static List<String> readFirstNLines(InputStream inputStream, int n) throws IOException { |
| | | List<String> lines = new ArrayList<>(); |
| | | |
| | | try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { |
| | | String line; |
| | | int count = 0; |
| | | |
| | | while ((line = reader.readLine()) != null && count < n) { |
| | | lines.add(line); |
| | | count++; |
| | | } |
| | | } |
| | | |
| | | return lines; |
| | | } |
| | | } |