InputStream
as a byte[]
.
*
* This method buffers the input internally, so there is no need to use a
* BufferedInputStream
.
*
* @param input the InputStream
to read from
* @return the requested byte array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}
public static long copy(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[1024 * 4];
long count = 0;
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void atomicCopy(File from, File to) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
File tmp = null;
try {
tmp = new File(to.getPath() + ".tmp");
in = new FileInputStream(from);
out = new FileOutputStream(tmp);
in.getChannel().transferTo(0, from.length(), out.getChannel());
out.close();
if (!tmp.renameTo(to)) {
throw new IOException("Failed to rename " + tmp + " to " + to);
}
Log.i(TAG, "Copied " + from + " to " + to);
} catch (IOException x) {
close(out);
delete(to);
throw x;
} finally {
close(in);
close(out);
delete(tmp);
}
}
public static void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Throwable x) {
// Ignored
}
}
public static boolean delete(File file) {
if (file != null && file.exists()) {
if (!file.delete()) {
Log.w(TAG, "Failed to delete file " + file);
return false;
}
Log.i(TAG, "Deleted file " + file);
}
return true;
}
public static void toast(Context context, int messageId) {
toast(context, messageId, true);
}
public static void toast(Context context, int messageId, boolean shortDuration) {
toast(context, context.getString(messageId), shortDuration);
}
public static void toast(Context context, String message) {
toast(context, message, true);
}
public static void toast(Context context, String message, boolean shortDuration) {
if (toast == null) {
toast = Toast.makeText(context, message, shortDuration ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(message);
toast.setDuration(shortDuration ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
}
toast.show();
}
/**
* Converts a byte-count to a formatted string suitable for display to the user.
* For instance:
* format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB".format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB".Broadcasts the given song info as the new song being played.
*/ public static void broadcastNewTrackInfo(Context context, MusicDirectory.Entry song) { Intent intent = new Intent(EVENT_META_CHANGED); if (song != null) { intent.putExtra("title", song.getTitle()); intent.putExtra("artist", song.getArtist()); intent.putExtra("album", song.getAlbum()); File albumArtFile = FileUtil.getAlbumArtFile(context, song); intent.putExtra("coverart", albumArtFile.getAbsolutePath()); } else { intent.putExtra("title", ""); intent.putExtra("artist", ""); intent.putExtra("album", ""); intent.putExtra("coverart", ""); } context.sendBroadcast(intent); } /** *Broadcasts the given player state as the one being set.
*/ public static void broadcastPlaybackStatusChange(Context context, PlayerState state) { Intent intent = new Intent(EVENT_PLAYSTATE_CHANGED); switch (state) { case STARTED: intent.putExtra("state", "play"); break; case STOPPED: intent.putExtra("state", "stop"); break; case PAUSED: intent.putExtra("state", "pause"); break; case COMPLETED: intent.putExtra("state", "complete"); break; default: return; // No need to broadcast. } context.sendBroadcast(intent); } public static void linkButtons(Context context, RemoteViews views, boolean playerActive) { Intent intent = new Intent(context, playerActive ? DownloadActivity.class : MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.appwidget_coverart, pendingIntent); views.setOnClickPendingIntent(R.id.appwidget_top, pendingIntent); // Emulate media button clicks. intent = new Intent("1"); intent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_play, pendingIntent); intent = new Intent("2"); intent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT)); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_next, pendingIntent); intent = new Intent("3"); intent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS)); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_previous, pendingIntent); intent = new Intent("4"); intent.setComponent(new ComponentName(context, DownloadServiceImpl.class)); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP)); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_stop, pendingIntent); } public static int getNetworkTimeout(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_NETWORK_TIMEOUT, "15000")); } public static int getDefaultAlbums(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_DEFAULT_ALBUMS, "5")); } public static int getMaxAlbums(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_MAX_ALBUMS, "20")); } public static int getDefaultSongs(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_DEFAULT_SONGS, "10")); } public static int getMaxSongs(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_MAX_SONGS, "25")); } public static int getMaxArtists(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_MAX_ARTISTS, "10")); } public static int getDefaultArtists(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_DEFAULT_ARTISTS, "3")); } public static int getBufferLength(Context context) { SharedPreferences prefs = getPreferences(context); return Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_BUFFER_LENGTH, "5")); } public static boolean getMediaButtonsPreference(Context context) { SharedPreferences prefs = getPreferences(context); return prefs.getBoolean(Constants.PREFERENCES_KEY_MEDIA_BUTTONS, true); } }