Use a constant for encoding

Replace string.getBytes("UTF-8") with
string.getBytes(CONSTANT).
master
jvoisin 5 years ago
parent 117e8c7fd1
commit bc832d0034
  1. 3
      airsonic-main/src/main/java/org/airsonic/player/controller/DownloadController.java
  2. 3
      airsonic-main/src/main/java/org/airsonic/player/io/ShoutCastOutputStream.java
  3. 11
      airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java
  4. 7
      airsonic-main/src/test/java/org/airsonic/player/service/PlaylistServiceTestImport.java

@ -41,6 +41,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
@ -178,7 +179,7 @@ public class DownloadController implements LastModified {
}
private String encodeAsRFC5987(String string) throws UnsupportedEncodingException {
byte[] stringAsByteArray = string.getBytes("UTF-8");
byte[] stringAsByteArray = string.getBytes(StandardCharsets.UTF_8);
char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] attrChar = {'!', '#', '$', '&', '+', '-', '.', '^', '_', '`', '|', '~', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
StringBuilder sb = new StringBuilder();

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* Implements SHOUTcast support by decorating an existing output stream.
@ -188,7 +189,7 @@ public class ShoutCastOutputStream extends OutputStream {
}
title = "StreamTitle='" + title + "';";
return title.getBytes("US-ASCII");
return title.getBytes(StandardCharsets.US_ASCII);
}
/**

@ -28,6 +28,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.*;
import java.util.*;
@ -385,11 +386,7 @@ public final class StringUtil {
return null;
}
byte[] utf8;
try {
utf8 = s.getBytes(ENCODING_UTF8);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x);
}
utf8 = s.getBytes(StandardCharsets.UTF_8);
return String.valueOf(Hex.encodeHex(utf8));
}
@ -404,7 +401,7 @@ public final class StringUtil {
if (s == null) {
return null;
}
return new String(Hex.decodeHex(s.toCharArray()), ENCODING_UTF8);
return new String(Hex.decodeHex(s.toCharArray()), StandardCharsets.UTF_8);
}
/**
@ -420,7 +417,7 @@ public final class StringUtil {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return new String(Hex.encodeHex(md5.digest(s.getBytes(ENCODING_UTF8))));
return new String(Hex.encodeHex(md5.digest(s.getBytes(StandardCharsets.UTF_8))));
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}

@ -25,6 +25,7 @@ import org.mockito.stubbing.Answer;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
@ -93,7 +94,7 @@ public class PlaylistServiceTestImport {
builder.append(mf3.getAbsolutePath()).append("\n");
doAnswer(new PersistPlayList(23)).when(playlistDao).createPlaylist(any());
doAnswer(new MediaFileHasEverything()).when(mediaFileService).getMediaFile(any(File.class));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
String path = "/path/to/"+playlistName+".m3u";
playlistService.importPlaylist(username, playlistName, path, inputStream, null);
verify(playlistDao).createPlaylist(actual.capture());
@ -127,7 +128,7 @@ public class PlaylistServiceTestImport {
builder.append("File3=").append(mf3.getAbsolutePath()).append("\n");
doAnswer(new PersistPlayList(23)).when(playlistDao).createPlaylist(any());
doAnswer(new MediaFileHasEverything()).when(mediaFileService).getMediaFile(any(File.class));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
String path = "/path/to/"+playlistName+".pls";
playlistService.importPlaylist(username, playlistName, path, inputStream, null);
verify(playlistDao).createPlaylist(actual.capture());
@ -164,7 +165,7 @@ public class PlaylistServiceTestImport {
builder.append(" </trackList>\n" + "</playlist>\n");
doAnswer(new PersistPlayList(23)).when(playlistDao).createPlaylist(any());
doAnswer(new MediaFileHasEverything()).when(mediaFileService).getMediaFile(any(File.class));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
InputStream inputStream = new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
String path = "/path/to/"+playlistName+".xspf";
playlistService.importPlaylist(username, playlistName, path, inputStream, null);
verify(playlistDao).createPlaylist(actual.capture());

Loading…
Cancel
Save