use of com.google.common.io.LineReader in project cdap by caskdata.
the class CommandPortService method serve.
/**
* Starts accepting incoming request. This method would block until this service is stopped.
*
* @throws IOException If any I/O error occurs on the socket connection.
*/
private void serve() throws IOException {
while (isRunning()) {
try {
Socket socket = serverSocket.accept();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
try {
// Read the client command and dispatch
String command = new LineReader(new InputStreamReader(socket.getInputStream(), "UTF-8")).readLine();
CommandHandler handler = handlers.get(command);
if (handler != null) {
try {
handler.handle(writer);
} catch (Throwable t) {
LOG.error(String.format("Exception thrown from CommandHandler for command %s", command), t);
}
}
} finally {
writer.flush();
socket.close();
}
} catch (Throwable th) {
// NOTE: catch any exception to keep the main service running
// Trigger by serverSocket.close() through the call from stop().
LOG.debug(th.getMessage(), th);
}
}
}
use of com.google.common.io.LineReader in project opencast by opencast.
the class VideoSegmenterServiceImpl method runSegmentationFFmpeg.
/**
* Does the actual segmentation with an FFmpeg call, adds the segments to the given videoContent of a catalog and
* returns a list with the resulting segments
*
* @param track the element to analyze
* @param videoContent the videoContent of the Mpeg7Catalog that the segments should be added to
* @param mediaFile the file of the track to analyze
* @param changesThreshold the changesThreshold that is used as option for the FFmpeg call
* @return a list of the resulting segments
* @throws IOException
*/
protected LinkedList<Segment> runSegmentationFFmpeg(Track track, Video videoContent, File mediaFile, float changesThreshold) throws IOException {
String[] command = new String[] { binary, "-nostats", "-i", mediaFile.getAbsolutePath().replaceAll(" ", "\\ "), "-filter:v", "select=gt(scene\\," + changesThreshold + "),showinfo", "-f", "null", "-" };
String commandline = StringUtils.join(command, " ");
logger.info("Running {}", commandline);
ProcessBuilder pbuilder = new ProcessBuilder(command);
List<String> segmentsStrings = new LinkedList<String>();
Process process = pbuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
try {
LineReader lr = new LineReader(reader);
String line = lr.readLine();
while (null != line) {
if (line.startsWith("[Parsed_showinfo")) {
segmentsStrings.add(line);
}
line = lr.readLine();
}
} catch (IOException e) {
logger.error("Error executing ffmpeg: {}", e.getMessage());
} finally {
reader.close();
}
// [Parsed_showinfo_1 @ 0x157fb40] n:0 pts:12 pts_time:12 pos:227495
// fmt:rgb24 sar:0/1 s:320x240 i:P iskey:1 type:I checksum:8DF39EA9
// plane_checksum:[8DF39EA9]
int segmentcount = 1;
LinkedList<Segment> segments = new LinkedList<Segment>();
if (segmentsStrings.size() == 0) {
Segment s = videoContent.getTemporalDecomposition().createSegment("segment-" + segmentcount);
s.setMediaTime(new MediaRelTimeImpl(0, track.getDuration()));
segments.add(s);
} else {
long starttime = 0;
long endtime = 0;
Pattern pattern = Pattern.compile("pts_time\\:\\d+(\\.\\d+)?");
for (String seginfo : segmentsStrings) {
Matcher matcher = pattern.matcher(seginfo);
String time = "0";
while (matcher.find()) {
time = matcher.group().substring(9);
}
endtime = (long) (Float.parseFloat(time) * 1000);
long segmentLength = endtime - starttime;
if (1000 * stabilityThresholdPrefilter < segmentLength) {
Segment segment = videoContent.getTemporalDecomposition().createSegment("segment-" + segmentcount);
segment.setMediaTime(new MediaRelTimeImpl(starttime, endtime - starttime));
segments.add(segment);
segmentcount++;
starttime = endtime;
}
}
// Add last segment
Segment s = videoContent.getTemporalDecomposition().createSegment("segment-" + segmentcount);
s.setMediaTime(new MediaRelTimeImpl(endtime, track.getDuration() - endtime));
segments.add(s);
}
logger.info("Segmentation of {} yields {} segments", mediaFile.toURI().toURL(), segments.size());
return segments;
}
use of com.google.common.io.LineReader in project Truck-Factor by aserg-ufmg.
the class Alias method readFile.
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]);
}
use of com.google.common.io.LineReader in project Truck-Factor by aserg-ufmg.
the class FileInfoReader method getFileInfo.
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;
}
use of com.google.common.io.LineReader in project maven-plugins by apache.
the class ServicesResourceTransformer method processResource.
public void processResource(String resource, InputStream is, final List<Relocator> relocators) throws IOException {
ServiceStream out = serviceEntries.get(resource);
if (out == null) {
out = new ServiceStream();
serviceEntries.put(resource, out);
}
final ServiceStream fout = out;
final String content = IOUtils.toString(is);
StringReader reader = new StringReader(content);
LineReader lineReader = new LineReader(reader);
String line;
while ((line = lineReader.readLine()) != null) {
String relContent = line;
for (Relocator relocator : relocators) {
if (relocator.canRelocateClass(relContent)) {
relContent = relocator.applyToSourceContent(relContent);
}
}
fout.append(relContent + "\n");
}
if (this.relocators == null) {
this.relocators = relocators;
}
}
Aggregations