public static void main(String[] args) { Buffer buf = null; if (args.length == 0) { System.out.println("引数がありません"); System.exit(1); } // Empty buf = LineBuffer.getInstance(); System.out.println("Empty Buffer Created"); System.out.println("size = " + buf.lengthByLine()); for (int i = 0; i < buf.lengthByLine(); i++) { System.out.print(buf.readLine(i)); } // From File File file = new File(args[0]); try { buf = LineBuffer.getInstance(file); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Buffer Created From File"); System.out.println("size = " + buf.lengthByLine()); for (int i = 0; i < buf.lengthByLine(); i++) { System.out.print(buf.readLine(i)); } // From Reader1 try { buf = LineBuffer.getInstance(new StringReader("aaa\nbbb\nccc\nddd\n")); } catch (IOException e) { e.printStackTrace(); } System.out.println("Buffer Created From Reader"); System.out.println("size = " + buf.lengthByLine()); for (int i = 0; i < buf.lengthByLine(); i++) { System.out.print(buf.readLine(i)); } // From Reader2 try { buf = LineBuffer.getInstance(new StringReader("aaa\nbbb\nccc\nddd")); } catch (IOException e) { e.printStackTrace(); } System.out.println("Buffer Created From Reader"); System.out.println("size = " + buf.lengthByLine()); for (int i = 0; i < buf.lengthByLine(); i++) { System.out.print(buf.readLine(i)); } }
/** * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a * linefeed ({@code "\r\n"}). * * @return a {@code String} containing the contents of the line, not including any * line-termination characters, or {@code null} if the end of the stream has been reached. * @throws IOException if an I/O error occurs */ public String readLine() throws IOException { while (lines.peek() == null) { cbuf.clear(); // The default implementation of Reader#read(CharBuffer) allocates a // temporary char[], so we call Reader#read(char[], int, int) instead. int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf); if (read == -1) { lineBuf.finish(); break; } lineBuf.add(buf, 0, read); } return lines.poll(); }
/** * Read a Line ended by CR, LF or CRLF. Read a line into a shared LineBuffer instance. The * LineBuffer is resused between calls and should not be held by the caller. The default or * supplied encoding is used to convert bytes to characters. * * @param len Maximum length of a line, or 0 for default * @return LineBuffer instance or null for EOF * @throws IOException */ public LineBuffer readLineBuffer(int len) throws IOException { len = fillLine(len > 0 ? len : buf.length); if (len < 0) return null; if (len == 0) { lineBuffer.size = 0; return lineBuffer; } byteBuffer.setStream(mark, len); lineBuffer.size = 0; int read = 0; while (read < len && reader.ready()) { int r = reader.read(lineBuffer.buffer, read, len - read); if (r <= 0) break; read += r; } lineBuffer.size = read; mark = -1; return lineBuffer; }