下面列出了com.google.common.io.LineReader#readLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected TypedCheckResult parse(Response response) throws IOException {
LineReader reader = new LineReader(new StringReader(response.getResponseBody(Constants.UTF_8.name())));
Map<Meta, VersionProfile> result = new HashMap<Meta, VersionProfile>();
String line;
try {
while ((line = reader.readLine()) != null) {
append(result, line);
}
} catch (IOException e) {
//ignore
}
if (Constants.PULL.equals(response.getHeader(Constants.UPDATE_TYPE))) {
return new TypedCheckResult(result, TypedCheckResult.Type.PULL);
} else {
return new TypedCheckResult(result, TypedCheckResult.Type.UPDATE);
}
}
public String blame(XmlDocument xmlDocument)
throws IOException, SAXException, ParserConfigurationException {
ImmutableMultimap<Integer, Record> resultingSourceMapping =
getResultingSourceMapping(xmlDocument);
LineReader lineReader = new LineReader(
new StringReader(xmlDocument.prettyPrint()));
StringBuilder actualMappings = new StringBuilder();
String line;
int count = 1;
while ((line = lineReader.readLine()) != null) {
actualMappings.append(count).append(line).append("\n");
if (resultingSourceMapping.containsKey(count)) {
for (Record record : resultingSourceMapping.get(count)) {
actualMappings.append(count).append("-->")
.append(record.getActionLocation().toString())
.append("\n");
}
}
count++;
}
return actualMappings.toString();
}
private MessageCheckpoint parseV2(final LineReader reader) throws IOException {
final long offset = Long.parseLong(reader.readLine());
final Map<String, Long> sequences = new HashMap<>();
while (true) {
final String line = reader.readLine();
if (Strings.isNullOrEmpty(line)) {
break;
}
final List<String> parts = SLASH_SPLITTER.splitToList(line);
final String subject = parts.get(0);
final long maxSequence = Long.parseLong(parts.get(1));
sequences.put(subject, maxSequence);
}
return new MessageCheckpoint(offset, sequences);
}
public String blame(XmlDocument xmlDocument)
throws IOException, SAXException, ParserConfigurationException {
ImmutableMultimap<Integer, Record> resultingSourceMapping =
getResultingSourceMapping(xmlDocument);
LineReader lineReader = new LineReader(
new StringReader(xmlDocument.prettyPrint()));
StringBuilder actualMappings = new StringBuilder();
String line;
int count = 0;
while ((line = lineReader.readLine()) != null) {
actualMappings.append(count + 1).append(line).append("\n");
if (resultingSourceMapping.containsKey(count)) {
for (Record record : resultingSourceMapping.get(count)) {
actualMappings.append(count + 1).append("-->")
.append(record.getActionLocation().toString())
.append("\n");
}
}
count++;
}
return actualMappings.toString();
}
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException {
Set<Integer> instances = Sets.newHashSet();
for (Discoverable discoverable : discoverables) {
InetSocketAddress socketAddress = discoverable.getSocketAddress();
try (Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort())) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
String msg = "Failure";
writer.println(msg);
String line = reader.readLine();
Assert.assertTrue(line.endsWith(msg));
instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length())));
}
}
return instances;
}
public static List<Alias> getAliasFromFile(String fileName) throws IOException{
List<Alias> fileAliases = new ArrayList<Alias>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
LineReader lineReader = new LineReader(br);
String sCurrentLine;
String[] values;
int countcfs = 0;
while ((sCurrentLine = lineReader.readLine()) != null) {
values = sCurrentLine.split(";");
if (values.length<3)
System.err.println("Erro na linha " + countcfs);
String rep = values[0];
String dev1 = values[1];
String dev2 = values[2];
fileAliases.add(new Alias(rep, dev1, dev2));
countcfs++;
}
return fileAliases;
}
private static Alias[] readFile(String fileName) throws IOException{
List<Alias> fileAliases = new ArrayList<Alias>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
LineReader lineReader = new LineReader(br);
String sCurrentLine;
String[] values;
int countcfs = 0;
while ((sCurrentLine = lineReader.readLine()) != null) {
values = sCurrentLine.split(";");
if (values.length<3)
System.err.println("Erro na linha " + countcfs);
String rep = values[0];
String dev1 = values[1];
String dev2 = values[2];
fileAliases.add(new Alias(rep, dev1, dev2));
countcfs++;
}
return fileAliases.toArray(new Alias[0]);
}
public static Map<String, List<LineInfo>> getFileInfo(String fileName) throws IOException{
Map<String, List<LineInfo>> fileInfoMap = new HashMap<String, List<LineInfo>>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
LineReader lineReader = new LineReader(br);
String sCurrentLine;
String[] values;
int countcfs = 0;
while ((sCurrentLine = lineReader.readLine()) != null) {
if (sCurrentLine.startsWith("#"))
continue;
values = sCurrentLine.split(";");
if (values.length<3)
System.err.println("Erro na linha " + countcfs);
String rep = values[0];
if (!fileInfoMap.containsKey(rep)) {
fileInfoMap.put(rep, new ArrayList<LineInfo>());
}
fileInfoMap.get(rep).add(new LineInfo(rep, Arrays.asList(values).subList(1, values.length)));
}
//lineReader.close();
return fileInfoMap;
}
private boolean addUrlDirToZipRecursively(ZipOutputStream zout, String root, String item, InputStream itemFound, Predicate<? super String> filter) throws IOException {
LineReader lr = new LineReader(new InputStreamReader(itemFound));
boolean readSubdirFile = false;
while (true) {
String line = lr.readLine();
if (line==null) {
// at end of file return true if we were able to recurse, else false
return readSubdirFile;
}
boolean isFile = addUrlItemRecursively(zout, root, item+"/"+line, filter);
if (isFile) {
readSubdirFile = true;
} else {
if (!readSubdirFile) {
// not a folder
return false;
} else {
// previous entry suggested it was a folder, but this one didn't work! -- was a false positive
// but zip will be in inconsistent state, so throw
throw new IllegalStateException("Failed to read entry "+line+" in "+item+" but previous entry implied it was a directory");
}
}
}
}
public GuidDatasetUrnStateStoreNameParser(FileSystem fs, Path jobStatestoreRootDir)
throws IOException {
this.fs = fs;
this.sanitizedNameToDatasetURNMap = Maps.synchronizedBiMap(HashBiMap.<String, String>create());
this.versionIdentifier = new Path(jobStatestoreRootDir, StateStoreNameVersion.V1.getDatasetUrnNameMapFile());
if (this.fs.exists(versionIdentifier)) {
this.version = StateStoreNameVersion.V1;
try (InputStream in = this.fs.open(versionIdentifier)) {
LineReader lineReader = new LineReader(new InputStreamReader(in, Charsets.UTF_8));
String shortenName = lineReader.readLine();
while (shortenName != null) {
String datasetUrn = lineReader.readLine();
this.sanitizedNameToDatasetURNMap.put(shortenName, datasetUrn);
shortenName = lineReader.readLine();
}
}
} else {
this.version = StateStoreNameVersion.V0;
}
}
@NonNull
public String blame(@NonNull XmlDocument xmlDocument)
throws IOException, SAXException, ParserConfigurationException {
ImmutableMultimap<Integer, Record> resultingSourceMapping =
getResultingSourceMapping(xmlDocument);
LineReader lineReader = new LineReader(
new StringReader(xmlDocument.prettyPrint()));
StringBuilder actualMappings = new StringBuilder();
String line;
int count = 0;
while ((line = lineReader.readLine()) != null) {
actualMappings.append(count + 1).append(line).append("\n");
if (resultingSourceMapping.containsKey(count)) {
for (Record record : resultingSourceMapping.get(count)) {
actualMappings.append(count + 1).append("-->")
.append(record.getActionLocation().toString())
.append("\n");
}
}
count++;
}
return actualMappings.toString();
}
public void read(Reader reader) throws IOException {
ObjectMapper om = new ObjectMapper();
LineReader lineReader = new LineReader(reader);
String line;
while ((line = lineReader.readLine()) != null) {
JsonLine jsonLine = om.readValue(line, JsonLine.class);
data.put(
jsonLine.linkedResource,
new BindResult(jsonLine.linkedResource, jsonLine.messageName, jsonLine.serviceName));
}
}
private ActionCheckpoint parseV3(LineReader reader) throws IOException {
final long offset = Long.parseLong(reader.readLine());
final Table<String, String, ConsumerGroupProgress> progresses = HashBasedTable.create();
while (true) {
final String subjectLine = reader.readLine();
if (Strings.isNullOrEmpty(subjectLine)) {
break;
}
final List<String> subjectParts = SLASH_SPLITTER.splitToList(subjectLine);
final String subject = subjectParts.get(0);
final int groupCount = Integer.parseInt(subjectParts.get(1));
for (int i = 0; i < groupCount; i++) {
final String groupLine = reader.readLine();
final List<String> groupParts = SLASH_SPLITTER.splitToList(groupLine);
final String group = groupParts.get(0);
final boolean broadcast = short2Boolean(Short.parseShort(groupParts.get(1)));
final long maxPulledMessageSequence = Long.parseLong(groupParts.get(2));
final int consumerCount = Integer.parseInt(groupParts.get(3));
final ConsumerGroupProgress progress = new ConsumerGroupProgress(subject, group, broadcast, maxPulledMessageSequence, new HashMap<>(consumerCount));
progresses.put(subject, group, progress);
final Map<String, ConsumerProgress> consumers = progress.getConsumers();
for (int j = 0; j < consumerCount; j++) {
final String consumerLine = reader.readLine();
final List<String> consumerParts = SLASH_SPLITTER.splitToList(consumerLine);
final String consumerId = consumerParts.get(0);
final long pull = Long.parseLong(consumerParts.get(1));
final long ack = Long.parseLong(consumerParts.get(2));
consumers.put(consumerId, new ConsumerProgress(subject, group, consumerId, pull, ack));
}
}
}
return new ActionCheckpoint(offset, progresses);
}
@Test
public void exception() throws IOException {
encoder.start();
final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);
final String logMsg;
try {
throw new IllegalArgumentException("Example Exception");
} catch (final IllegalArgumentException e) {
logMsg = encodeToStr(new LoggingEvent(
LOGGER_NAME,
logger,
Level.DEBUG,
"message {}",
e,
new Object[]{1}));
}
final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
basicValidation(jsonNode);
final LineReader msg =
new LineReader(new StringReader(jsonNode.get("full_message").textValue()));
assertEquals("message 1", msg.readLine());
assertEquals("java.lang.IllegalArgumentException: Example Exception", msg.readLine());
final String line = msg.readLine();
assertTrue(line.matches("^\tat de.siegmar.logbackgelf.GelfEncoderTest.exception"
+ "\\(GelfEncoderTest.java:\\d+\\)$"), "Unexpected line: " + line);
}
/**
* 使用LineReader
* @throws Exception
*/
@Test
public void example6() throws Exception{
File file = new File("src/main/resources/sample.txt");
LineReader lineReader = new LineReader(new FileReader(file));
for(String line = lineReader.readLine();line!=null;line=lineReader.readLine()){
System.out.println(line);
}
}
/**
* Test of getFile method, of class MultimediaObject.
*/
@Test
public void testAnalyzeNewLine1() {
System.out.println("analyzeNewLine 1");
File file = new File("src/test/resources/testoutput1.txt");
ConversionOutputAnalyzer oa1= new ConversionOutputAnalyzer(0, null);
try
{
FileInputStream fis = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fis, "UTF-8");
LineReader reader = new LineReader(streamReader);
String sLine = null;
while ((sLine = reader.readLine()) != null)
{
oa1.analyzeNewLine(sLine);
}
String result= oa1.getLastWarning();
String expResult= null;
assertEquals(expResult, result);
}
catch (IOException ioError)
{
System.out.println("IO error "+ioError.getMessage());
ioError.printStackTrace();
throw new AssertionError("IO error "+ioError.getMessage());
}
catch (EncoderException enError)
{
System.out.println("Encoder error "+enError.getMessage());
enError.printStackTrace();
throw new AssertionError("Encoder error "+enError.getMessage());
}
}
/**
* Test of getFile method, of class MultimediaObject.
*/
@Test
public void testAnalyzeNewLine1() {
System.out.println("analyzeNewLine 1");
File file = new File(getResourceSourcePath(), "testoutput1.txt");
ConversionOutputAnalyzer oa1= new ConversionOutputAnalyzer(0, null);
try
{
FileInputStream fis = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fis, "UTF-8");
LineReader reader = new LineReader(streamReader);
String sLine = null;
while ((sLine = reader.readLine()) != null)
{
oa1.analyzeNewLine(sLine);
}
String result= oa1.getLastWarning();
String expResult= null;
assertEquals(expResult, result);
}
catch (IOException ioError)
{
System.out.println("IO error "+ioError.getMessage());
ioError.printStackTrace();
throw new AssertionError("IO error "+ioError.getMessage());
}
catch (EncoderException enError)
{
System.out.println("Encoder error "+enError.getMessage());
enError.printStackTrace();
throw new AssertionError("Encoder error "+enError.getMessage());
}
}