1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import com.google.common.base.Function; 20 import com.google.common.collect.Lists; 21 import java.io.BufferedReader; 22 import java.io.FilterReader; 23 import java.io.IOException; 24 import java.io.Reader; 25 import java.io.StringReader; 26 import java.nio.CharBuffer; 27 import java.util.Arrays; 28 import java.util.List; 29 30 /** 31 * Unit tests for {@link LineBuffer} and {@link LineReader}. 32 * 33 * @author Chris Nokleberg 34 */ 35 @AndroidIncompatible // occasionally very slow 36 public class LineBufferTest extends IoTestCase { 37 testProcess()38 public void testProcess() throws IOException { 39 bufferHelper(""); 40 bufferHelper("\n", "\n"); 41 bufferHelper("\r\n", "\r\n"); 42 bufferHelper("\n\r", "\n", "\r"); 43 bufferHelper("\r", "\r"); 44 bufferHelper("\n\n", "\n", "\n"); 45 bufferHelper("\r\n\r\n", "\r\n", "\r\n"); 46 bufferHelper("\r\r", "\r", "\r"); 47 bufferHelper("\ra\r\n\n\r\r", "\r", "a\r\n", "\n", "\r", "\r"); 48 bufferHelper("no newlines at all", "no newlines at all"); 49 bufferHelper("two lines\nbut no newline at end", "two lines\n", "but no newline at end"); 50 bufferHelper( 51 "\nempty first line\nno newline at end", "\n", "empty first line\n", "no newline at end"); 52 bufferHelper("three\rlines\rno newline at end", "three\r", "lines\r", "no newline at end"); 53 bufferHelper("mixed\nline\rendings\r\n", "mixed\n", "line\r", "endings\r\n"); 54 } 55 56 private static final int[] CHUNK_SIZES = {1, 2, 3, Integer.MAX_VALUE}; 57 bufferHelper(String input, String... expect)58 private static void bufferHelper(String input, String... expect) throws IOException { 59 60 List<String> expectProcess = Arrays.asList(expect); 61 List<String> expectRead = 62 Lists.transform( 63 expectProcess, 64 new Function<String, String>() { 65 @Override 66 public String apply(String value) { 67 return value.replaceAll("[\\r\\n]", ""); 68 } 69 }); 70 71 for (int chunk : CHUNK_SIZES) { 72 chunk = Math.max(1, Math.min(chunk, input.length())); 73 assertEquals(expectProcess, bufferHelper(input, chunk)); 74 assertEquals(expectRead, readUsingJava(input, chunk)); 75 assertEquals(expectRead, readUsingReader(input, chunk, true)); 76 assertEquals(expectRead, readUsingReader(input, chunk, false)); 77 } 78 } 79 bufferHelper(String input, int chunk)80 private static List<String> bufferHelper(String input, int chunk) throws IOException { 81 final List<String> lines = Lists.newArrayList(); 82 LineBuffer lineBuf = 83 new LineBuffer() { 84 @Override 85 protected void handleLine(String line, String end) { 86 lines.add(line + end); 87 } 88 }; 89 char[] chars = input.toCharArray(); 90 int off = 0; 91 while (off < chars.length) { 92 int len = Math.min(chars.length, off + chunk) - off; 93 lineBuf.add(chars, off, len); 94 off += len; 95 } 96 lineBuf.finish(); 97 return lines; 98 } 99 readUsingJava(String input, int chunk)100 private static List<String> readUsingJava(String input, int chunk) throws IOException { 101 BufferedReader r = new BufferedReader(getChunkedReader(input, chunk)); 102 List<String> lines = Lists.newArrayList(); 103 String line; 104 while ((line = r.readLine()) != null) { 105 lines.add(line); 106 } 107 r.close(); 108 return lines; 109 } 110 readUsingReader(String input, int chunk, boolean asReader)111 private static List<String> readUsingReader(String input, int chunk, boolean asReader) 112 throws IOException { 113 Readable readable = 114 asReader ? getChunkedReader(input, chunk) : getChunkedReadable(input, chunk); 115 LineReader r = new LineReader(readable); 116 List<String> lines = Lists.newArrayList(); 117 String line; 118 while ((line = r.readLine()) != null) { 119 lines.add(line); 120 } 121 return lines; 122 } 123 124 // Returns a Readable that is *not* a Reader. getChunkedReadable(String input, int chunk)125 private static Readable getChunkedReadable(String input, int chunk) { 126 final Reader reader = getChunkedReader(input, chunk); 127 return new Readable() { 128 @Override 129 public int read(CharBuffer cbuf) throws IOException { 130 return reader.read(cbuf); 131 } 132 }; 133 } 134 135 private static Reader getChunkedReader(String input, final int chunk) { 136 return new FilterReader(new StringReader(input)) { 137 @Override 138 public int read(char[] cbuf, int off, int len) throws IOException { 139 return super.read(cbuf, off, Math.min(chunk, len)); 140 } 141 }; 142 } 143 } 144