Java tutorial
/* * Digital Media Server, for streaming digital media to DLNA compatible devices * based on www.ps3mediaserver.org and www.universalmediaserver.com. * Copyright (C) 2016 Digital Media Server developers. * * This program is a free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License only. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package net.pms.io; import static org.apache.commons.lang3.StringUtils.isBlank; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.pms.service.Services; /** * A {@link ProcessWrapperConsumer} implementation that return the process * output as a {@link List} of {@link String}s wrapped in a * {@link ListProcessWrapperResult}. * * @author Nadahar */ public class ListProcessWrapperConsumer implements ProcessWrapperConsumer<ListProcessWrapperResult, List<String>> { private static final Logger LOGGER = LoggerFactory.getLogger(ListProcessWrapperConsumer.class); @Override @Nullable public FutureTask<List<String>> consume(InputStream inputStream) { return consume(inputStream, null); } @Override @Nullable public FutureTask<List<String>> consume(@Nullable final InputStream inputStream, @Nullable String threadName) { if (inputStream == null) { return null; } Callable<List<String>> callable = new Callable<List<String>>() { @Override public List<String> call() throws Exception { List<String> result = new ArrayList<>(); Charset outputCharset; if (Services.WINDOWS_CONSOLE != null) { outputCharset = Services.WINDOWS_CONSOLE; } else { outputCharset = StandardCharsets.UTF_8; } try (BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream, outputCharset))) { String line; while ((line = reader.readLine()) != null) { result.add(line); } } if (LOGGER.isTraceEnabled()) { for (String line : result) { LOGGER.trace("Process output: {}", line); } } return result; } }; FutureTask<List<String>> result = new FutureTask<List<String>>(callable); Thread runner; if (isBlank(threadName)) { runner = new Thread(result); } else { runner = new Thread(result, threadName); } runner.start(); return result; } @Override public ListProcessWrapperResult createResult(@Nullable List<String> output, int exitCode, @Nullable Throwable throwable) { return new ListProcessWrapperResult(output, exitCode, throwable); } }