下面列出了org.apache.commons.lang3.StringUtils#isAsciiPrintable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
if (logger.isDebugEnabled()) {
byte[] payload = message.getPayload();
String text = new String(payload, "UTF-8");
if (StringUtils.isAsciiPrintable(text)) {
logger.debug("Message arrived from topic {}. Payload: {}", new Object[] {topic, text});
} else {
logger.debug("Message arrived from topic {}. Binary value of size {}", new Object[] {topic, payload.length});
}
}
if (mqttQueue.size() >= maxQueueSize){
throw new IllegalStateException("The subscriber queue is full, cannot receive another message until the processor is scheduled to run.");
} else {
mqttQueue.add(new MQTTQueueMessage(topic, message));
}
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
if (logger.isDebugEnabled()) {
byte[] payload = message.getPayload();
String text = new String(payload, "UTF-8");
if (StringUtils.isAsciiPrintable(text)) {
logger.debug("Message arrived from topic {}. Payload: {}", new Object[] {topic, text});
} else {
logger.debug("Message arrived from topic {}. Binary value of size {}", new Object[] {topic, payload.length});
}
}
if (mqttQueue.size() >= maxQueueSize){
throw new IllegalStateException("The subscriber queue is full, cannot receive another message until the processor is scheduled to run.");
} else {
mqttQueue.add(new MQTTQueueMessage(topic, message));
}
}
private void loadData(File file) throws ServiceException, Exception {
if (StringUtils.isBlank(this.oid)) {
return;
}
DefaultResult<SysUploadVO> result = this.sysUploadService.findForNoByteContent(this.oid);
if (result.getValue()==null) {
throw new ControllerException(result.getSystemMessage().getValue());
}
SysUploadVO uploadData = result.getValue();
if (YesNo.YES.equals(uploadData.getIsFile())) {
file = UploadSupportUtils.getRealFile(this.oid);
if (!file.exists()) {
return;
}
this.inputStream = new ByteArrayInputStream( FileUtils.readFileToByteArray(file) );
this.filename = file.getName();
} else {
this.inputStream = new ByteArrayInputStream( UploadSupportUtils.getDataBytes(this.oid) );
this.filename = UploadSupportUtils.generateRealFileName(uploadData.getShowName());
}
if ("view".equals(this.type)) {
if (file!=null) {
try {
this.contentType = FSUtils.getMimeType(file);
} catch (Exception e) {
logger.warn( e.getMessage().toString() );
}
} else {
this.contentType = FSUtils.getMimeType(uploadData.getShowName());
}
}
if (!StringUtils.isAsciiPrintable(result.getValue().getShowName())) {
String userAgent = super.defaultString(super.getHttpServletRequest().getHeader("User-Agent")).toLowerCase();
if (userAgent.indexOf("firefox")==-1) { // for chrome or other browser.
this.filename = URLEncoder.encode(result.getValue().getShowName(), Constants.BASE_ENCODING);
}
return;
}
this.filename = result.getValue().getShowName();
}
/**
* @param inputPropsFile
* @param cleanPropsFile
* @param unusedKeys
* @throws IOException
*/
private static void outputCleanedProperties(File inputPropsFile, File cleanPropsFile,
Collection<String> unusedKeys) throws IOException
{
List<String> lines;
try (BufferedReader reader = new BufferedReader(new FileReader(inputPropsFile, StandardCharsets.UTF_8)))
{
lines = reader.lines().collect(Collectors.toList());
}
Writer writer = new BufferedWriter(new PrintWriter(cleanPropsFile, StandardCharsets.UTF_8));
writer.write("# " + PROPERTIES_FILE
+ " with all unused keys removed as at "
+ LocalDateTime.now(Clock.systemUTC())
+ "\n");
boolean lastLineBlank = false;
for (String line : lines)
{
boolean found;
if (lastLineBlank && line.trim().isEmpty())
{
continue;
}
found = unusedKeys.stream().anyMatch(key -> line.startsWith(key + '='));
if (!found)
{
lastLineBlank = line.trim().isEmpty();
if (!StringUtils.isAsciiPrintable(line))
{
System.out.println("Found a non adcii line " + line);
}
writer.write(line + "\n");
}
}
writer.close();
}
/**
* @param inputPropsFile
* @param cleanPropsFile
* @param unusedKeys
* @throws IOException
*/
private static void outputCleanedProperties(File inputPropsFile, File cleanPropsFile,
Collection<String> unusedKeys) throws IOException
{
List<String> lines;
try (BufferedReader reader = new BufferedReader(new FileReader(inputPropsFile, StandardCharsets.UTF_8)))
{
lines = reader.lines().collect(Collectors.toList());
}
Writer writer = new BufferedWriter(new PrintWriter(cleanPropsFile, StandardCharsets.UTF_8));
writer.write("# " + PROPERTIES_FILE
+ " with all unused keys removed as at "
+ LocalDateTime.now(Clock.systemUTC())
+ "\n");
boolean lastLineBlank = false;
for (String line : lines)
{
boolean found;
if (lastLineBlank && line.trim().isEmpty())
{
continue;
}
found = unusedKeys.stream().anyMatch(key -> line.startsWith(key + '='));
if (!found)
{
lastLineBlank = line.trim().isEmpty();
if (!StringUtils.isAsciiPrintable(line))
{
System.out.println("Found a non adcii line " + line);
}
writer.write(line + "\n");
}
}
writer.close();
}
/**
* Quick and dirty singular to plural conversion.
* @param singul a word
* @return a guess of its plural form
*/
public static String singularToPlural(String singul) {
if (!StringUtils.isAsciiPrintable(singul)) {
return singul;
}
return (StringUtils.isBlank(singul) || singul.endsWith("es") || singul.endsWith("ies")) ? singul :
(singul.endsWith("s") ? singul + "es" :
(singul.endsWith("y") ? StringUtils.removeEndIgnoreCase(singul, "y") + "ies" :
singul + "s"));
}
/**
* Creates a transcoded input stream by interpreting the given command line string.
* This includes the following:
* <ul>
* <li>Splitting the command line string to an array.</li>
* <li>Replacing occurrences of "%s" with the path of the given music file.</li>
* <li>Replacing occurrences of "%t" with the title of the given music file.</li>
* <li>Replacing occurrences of "%l" with the album name of the given music file.</li>
* <li>Replacing occurrences of "%a" with the artist name of the given music file.</li>
* <li>Replacing occurrcences of "%b" with the max bitrate.</li>
* <li>Replacing occurrcences of "%o" with the video time offset (used for scrubbing).</li>
* <li>Replacing occurrcences of "%d" with the video duration (used for HLS).</li>
* <li>Replacing occurrcences of "%w" with the video image width.</li>
* <li>Replacing occurrcences of "%h" with the video image height.</li>
* <li>Prepending the path of the transcoder directory if the transcoder is found there.</li>
* </ul>
*
* @param command The command line string.
* @param maxBitRate The maximum bitrate to use. May not be {@code null}.
* @param videoTranscodingSettings Parameters used when transcoding video. May be {@code null}.
* @param mediaFile The media file.
* @param in Data to feed to the process. May be {@code null}. @return The newly created input stream.
*/
private TranscodeInputStream createTranscodeInputStream(String command, Integer maxBitRate,
VideoTranscodingSettings videoTranscodingSettings, MediaFile mediaFile, InputStream in) throws IOException {
String title = mediaFile.getTitle();
String album = mediaFile.getAlbumName();
String artist = mediaFile.getArtist();
if (title == null) {
title = "Unknown Song";
}
if (album == null) {
album = "Unknown Album";
}
if (artist == null) {
artist = "Unknown Artist";
}
List<String> result = new LinkedList<String>(Arrays.asList(StringUtil.split(command)));
result.set(0, getTranscodeDirectory().resolve(result.get(0)).toString());
Path tmpFile = null;
for (int i = 1; i < result.size(); i++) {
String cmd = result.get(i);
if (cmd.contains("%b")) {
cmd = cmd.replace("%b", String.valueOf(maxBitRate));
}
if (cmd.contains("%t")) {
cmd = cmd.replace("%t", title);
}
if (cmd.contains("%l")) {
cmd = cmd.replace("%l", album);
}
if (cmd.contains("%a")) {
cmd = cmd.replace("%a", artist);
}
if (cmd.contains("%o") && videoTranscodingSettings != null) {
cmd = cmd.replace("%o", String.valueOf(videoTranscodingSettings.getTimeOffset()));
}
if (cmd.contains("%d") && videoTranscodingSettings != null) {
cmd = cmd.replace("%d", String.valueOf(videoTranscodingSettings.getDuration()));
}
if (cmd.contains("%w") && videoTranscodingSettings != null) {
cmd = cmd.replace("%w", String.valueOf(videoTranscodingSettings.getWidth()));
}
if (cmd.contains("%h") && videoTranscodingSettings != null) {
cmd = cmd.replace("%h", String.valueOf(videoTranscodingSettings.getHeight()));
}
if (cmd.contains("%s")) {
// Work-around for filename character encoding problem on Windows.
// Create temporary file, and feed this to the transcoder.
Path path = mediaFile.getFile().toAbsolutePath();
if (Util.isWindows() && !mediaFile.isVideo() && !StringUtils.isAsciiPrintable(path.toString())) {
tmpFile = Files.createTempFile("airsonic", "." + MoreFiles.getFileExtension(path));
tmpFile.toFile().deleteOnExit();
Files.copy(path, tmpFile, StandardCopyOption.REPLACE_EXISTING);
LOG.debug("Created tmp file: " + tmpFile);
cmd = cmd.replace("%s", tmpFile.toString());
} else {
cmd = cmd.replace("%s", path.toString());
}
}
result.set(i, cmd);
}
return new TranscodeInputStream(new ProcessBuilder(result), in, tmpFile);
}
/**
* Creates a transcoded input stream by interpreting the given command line string.
* This includes the following:
* <ul>
* <li>Splitting the command line string to an array.</li>
* <li>Replacing occurrences of "%s" with the path of the given music file.</li>
* <li>Replacing occurrences of "%t" with the title of the given music file.</li>
* <li>Replacing occurrences of "%l" with the album name of the given music file.</li>
* <li>Replacing occurrences of "%a" with the artist name of the given music file.</li>
* <li>Replacing occurrcences of "%b" with the max bitrate.</li>
* <li>Replacing occurrcences of "%o" with the video time offset (used for scrubbing).</li>
* <li>Replacing occurrcences of "%d" with the video duration (used for HLS).</li>
* <li>Replacing occurrcences of "%w" with the video image width.</li>
* <li>Replacing occurrcences of "%h" with the video image height.</li>
* <li>Prepending the path of the transcoder directory if the transcoder is found there.</li>
* </ul>
*
* @param command The command line string.
* @param maxBitRate The maximum bitrate to use. May not be {@code null}.
* @param videoTranscodingSettings Parameters used when transcoding video. May be {@code null}.
* @param mediaFile The media file.
* @param in Data to feed to the process. May be {@code null}. @return The newly created input stream.
*/
private TranscodeInputStream createTranscodeInputStream(String command, Integer maxBitRate,
VideoTranscodingSettings videoTranscodingSettings, MediaFile mediaFile, InputStream in) throws IOException {
String title = mediaFile.getTitle();
String album = mediaFile.getAlbumName();
String artist = mediaFile.getArtist();
if (title == null) {
title = "Unknown Song";
}
if (album == null) {
album = "Unknown Album";
}
if (artist == null) {
artist = "Unknown Artist";
}
List<String> result = new LinkedList<String>(Arrays.asList(StringUtil.split(command)));
result.set(0, getTranscodeDirectory().getPath() + File.separatorChar + result.get(0));
File tmpFile = null;
for (int i = 1; i < result.size(); i++) {
String cmd = result.get(i);
if (cmd.contains("%b")) {
cmd = cmd.replace("%b", String.valueOf(maxBitRate));
}
if (cmd.contains("%t")) {
cmd = cmd.replace("%t", title);
}
if (cmd.contains("%l")) {
cmd = cmd.replace("%l", album);
}
if (cmd.contains("%a")) {
cmd = cmd.replace("%a", artist);
}
if (cmd.contains("%o") && videoTranscodingSettings != null) {
cmd = cmd.replace("%o", String.valueOf(videoTranscodingSettings.getTimeOffset()));
}
if (cmd.contains("%d") && videoTranscodingSettings != null) {
cmd = cmd.replace("%d", String.valueOf(videoTranscodingSettings.getDuration()));
}
if (cmd.contains("%w") && videoTranscodingSettings != null) {
cmd = cmd.replace("%w", String.valueOf(videoTranscodingSettings.getWidth()));
}
if (cmd.contains("%h") && videoTranscodingSettings != null) {
cmd = cmd.replace("%h", String.valueOf(videoTranscodingSettings.getHeight()));
}
if (cmd.contains("%s")) {
// Work-around for filename character encoding problem on Windows.
// Create temporary file, and feed this to the transcoder.
String path = mediaFile.getFile().getAbsolutePath();
if (Util.isWindows() && !mediaFile.isVideo() && !StringUtils.isAsciiPrintable(path)) {
tmpFile = File.createTempFile("airsonic", "." + FilenameUtils.getExtension(path));
tmpFile.deleteOnExit();
FileUtils.copyFile(new File(path), tmpFile);
LOG.debug("Created tmp file: " + tmpFile);
cmd = cmd.replace("%s", tmpFile.getPath());
} else {
cmd = cmd.replace("%s", path);
}
}
result.set(i, cmd);
}
return new TranscodeInputStream(new ProcessBuilder(result), in, tmpFile);
}
@Override
protected boolean handle(CharSequence str) {
return StringUtils.isAsciiPrintable(str);
}
/** Returns true if the password is valid. */
public static boolean isPasswordValid(String value) {
return value != null && value.length() >= 8 && value.length() <= 20
&& StringUtils.isAsciiPrintable(value);
}
/**
* 値がASCII文字のみかチェックします。
*
* @param value
* @return
*/
public static boolean isAscii(String value) {
return StringUtils.isAsciiPrintable(value);
}