Skip to content

Instantly share code, notes, and snippets.

@sergeykomlach
Created February 8, 2019 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergeykomlach/b31c50089037746231a57f978d81b9c9 to your computer and use it in GitHub Desktop.
Save sergeykomlach/b31c50089037746231a57f978d81b9c9 to your computer and use it in GitHub Desktop.
import android.content.Context;
import java.io.ByteArrayInputStream;
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.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
public class test {
/*
* Для работы в пределах песочницы приложения можно использовать директории, получаемые из Context'a (контекст это наша активити к примеру)
*
* Смотри https://developer.android.com/reference/android/content/Context.html#getCacheDir() и ниже
*
*
* для доступа к памяти устройства (вне пределов песочницы) - нужно
* 1) Заделкарировать в манифесте приложения пермишины
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
* 2) Реализовать РантаймПермишн https://developer.android.com/training/permissions/requesting
* */
public static void writeToFile(Context context, byte[] data) {
try {
File file = createFile(context.getCacheDir(), "somefile.bin");
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
fastCopy(byteArrayInputStream, fileOutputStream);
byteArrayInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] readFromFile(Context context) {
try {
File file = createFile(context.getCacheDir(), "somefile.bin");
if (!file.exists()) {
return null;
}
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
fastCopy(fileInputStream, byteArrayOutputStream);
fileInputStream.close();
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static File createFile(File dir, String filename) {
if (!dir.exists())
dir.mkdirs();
return new File(dir, filename);
}
public static void copy(File src, File dst) throws IOException {
if (dst.exists()) {
dst.delete();
dst.createNewFile();
}
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
public static void fastCopy(final InputStream src, final OutputStream dest) throws IOException {
final ReadableByteChannel inputChannel = Channels.newChannel(src);
final WritableByteChannel outputChannel = Channels.newChannel(dest);
fastCopy(inputChannel, outputChannel);
inputChannel.close();
outputChannel.close();
}
public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
while (src.read(buffer) != -1) {
buffer.flip();
dest.write(buffer);
buffer.compact();
}
buffer.flip();
while (buffer.hasRemaining()) {
dest.write(buffer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment