mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-01-08 08:55:51 +01:00
1d41f2f8e4
The notfification APIs now use NotificationSpec as their only parameter, which contains all information (required and optional ones). We no longer have separate methods and actions for SMS/EMAIL/GENERIC anymore. The type of notification is important now, not how we received them technically.
74 lines
2.7 KiB
Java
74 lines
2.7 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.channels.FileChannel;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
public class FileUtils {
|
|
/**
|
|
* Copies the the given sourceFile to destFile, overwriting it, in case it exists.
|
|
*
|
|
* @param sourceFile
|
|
* @param destFile
|
|
* @throws IOException
|
|
*/
|
|
public static void copyFile(File sourceFile, File destFile) throws IOException {
|
|
if (!sourceFile.exists()) {
|
|
throw new IOException("Does not exist: " + sourceFile.getAbsolutePath());
|
|
}
|
|
copyFile(new FileInputStream(sourceFile), new FileOutputStream(destFile));
|
|
}
|
|
|
|
private static void copyFile(FileInputStream sourceStream, FileOutputStream destStream) throws IOException {
|
|
try (FileChannel fromChannel = sourceStream.getChannel(); FileChannel toChannel = destStream.getChannel()) {
|
|
fromChannel.transferTo(0, fromChannel.size(), toChannel);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the existing external storage dir.
|
|
*
|
|
* @throws IOException when the directory is not available
|
|
*/
|
|
public static File getExternalFilesDir() throws IOException {
|
|
File dir = GBApplication.getContext().getExternalFilesDir(null);
|
|
if (dir == null) {
|
|
throw new IOException("Unable to access external files dir: null");
|
|
}
|
|
if (!dir.exists() && !dir.mkdirs()) {
|
|
throw new IOException("Unable to access external files dir: does not exist");
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
/**
|
|
* Reads the contents of the given InputStream into a byte array, but does not
|
|
* read more than maxLen bytes. If the stream provides more than maxLen bytes,
|
|
* an IOException is thrown.
|
|
*
|
|
* @param in the stream to read from
|
|
* @param maxLen the maximum number of bytes to read/return
|
|
* @return the bytes read from the InputStream
|
|
* @throws IOException when reading failed or when maxLen was exceeded
|
|
*/
|
|
public static byte[] readAll(InputStream in, long maxLen) throws IOException {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(8192, in.available()));
|
|
byte[] buf = new byte[8192];
|
|
int read = 0;
|
|
long totalRead = 0;
|
|
while ((read = in.read(buf)) > 0) {
|
|
out.write(buf, 0, read);
|
|
totalRead += read;
|
|
if (totalRead > maxLen) {
|
|
throw new IOException("Too much data to read into memory. Got already " + totalRead + buf);
|
|
}
|
|
}
|
|
return out.toByteArray();
|
|
}
|
|
} |