Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachnid-27 committed Jan 31, 2017
1 parent 3cac53c commit 61fbd05
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,17 @@ protected void initView() {
}

@Override
protected void initData() {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_comic, menu);
mDownloadItem = menu.findItem(R.id.comic_switch_download);
if (ServiceUtils.isServiceRunning(getActivity(), DownloadService.class)) {
onDownloadStart();
} else {
onDownloadStop();
}
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_comic, menu);
mDownloadItem = menu.findItem(R.id.comic_switch_download);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
MessageDialogFragment fragment;
Expand Down
37 changes: 16 additions & 21 deletions app/src/main/java/com/hiroshi/cimoc/utils/DocumentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.net.Uri;
import android.support.v4.provider.DocumentFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
Expand All @@ -13,10 +15,6 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -166,31 +164,28 @@ public static void writeStringToFile(ContentResolver resolver, DocumentFile file
}

public static void writeBinaryToFile(ContentResolver resolver, DocumentFile file, InputStream input) throws IOException {
OutputStream output = resolver.openOutputStream(file.getUri());
BufferedInputStream inputStream = null;
BufferedOutputStream outputStream = null;

ReadableByteChannel inputChannel = null;
WritableByteChannel outputChannel = null;
try {
if (input != null && output != null) {
inputChannel = Channels.newChannel(input);
outputChannel = Channels.newChannel(output);

ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
while (inputChannel.read(buffer) != -1) {
buffer.flip();
outputChannel.write(buffer);
buffer.compact();
}
OutputStream output = resolver.openOutputStream(file.getUri());

if (output != null) {
inputStream = new BufferedInputStream(input, 8192);
outputStream = new BufferedOutputStream(output, 8192);

buffer.flip();
while (buffer.hasRemaining()) {
outputChannel.write(buffer);
int length;
byte[] buffer = new byte[8192];
while ((length = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, length);
}
output.flush();
} else {
closeStream(input);
throw new FileNotFoundException();
}
} finally {
closeStream(inputChannel, outputChannel);
closeStream(inputStream, outputStream);
}
}

Expand Down

0 comments on commit 61fbd05

Please sign in to comment.