Search in sources :

Example 6 with LineReader

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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LineReader(com.google.common.io.LineReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) BufferedWriter(java.io.BufferedWriter)

Example 7 with LineReader

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;
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) LinkedList(java.util.LinkedList) MediaTimePoint(org.opencastproject.metadata.mpeg7.MediaTimePoint) Segment(org.opencastproject.metadata.mpeg7.Segment) LineReader(com.google.common.io.LineReader) BufferedReader(java.io.BufferedReader) MediaRelTimeImpl(org.opencastproject.metadata.mpeg7.MediaRelTimeImpl)

Example 8 with LineReader

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]);
}
Also used : LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 9 with LineReader

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;
}
Also used : HashMap(java.util.HashMap) LineReader(com.google.common.io.LineReader) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader)

Example 10 with LineReader

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;
    }
}
Also used : LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) Relocator(org.apache.maven.plugins.shade.relocation.Relocator)

Aggregations

LineReader (com.google.common.io.LineReader)20 InputStreamReader (java.io.InputStreamReader)10 IOException (java.io.IOException)8 StringReader (java.io.StringReader)6 ArrayList (java.util.ArrayList)6 OutputStreamWriter (java.io.OutputStreamWriter)5 Socket (java.net.Socket)5 Discoverable (com.continuuity.weave.discovery.Discoverable)4 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 InputStream (java.io.InputStream)4 PrintWriter (java.io.PrintWriter)4 CommandOutputHandler (com.atlassian.stash.scm.CommandOutputHandler)3 Watchdog (com.atlassian.utils.process.Watchdog)3 WeaveController (com.continuuity.weave.api.WeaveController)3 WeaveRunner (com.continuuity.weave.api.WeaveRunner)3 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)3 Matcher (java.util.regex.Matcher)3 Test (org.junit.Test)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)2