|
11 | 11 | import java.io.BufferedInputStream;
|
12 | 12 | import java.io.ByteArrayOutputStream;
|
13 | 13 | import java.io.Closeable;
|
| 14 | +import java.io.DataInputStream; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.FileNotFoundException; |
14 | 18 | import java.io.IOException;
|
15 | 19 | import java.io.InputStream;
|
| 20 | +import java.io.InputStreamReader; |
16 | 21 | import java.io.OutputStream;
|
17 | 22 | import java.io.OutputStreamWriter;
|
| 23 | +import java.io.UnsupportedEncodingException; |
18 | 24 | import java.net.CookieHandler;
|
19 | 25 | import java.net.CookieManager;
|
20 | 26 | import java.net.HttpURLConnection;
|
21 | 27 | import java.net.MalformedURLException;
|
22 | 28 | import java.net.URL;
|
| 29 | +import java.nio.CharBuffer; |
23 | 30 | import java.util.ArrayList;
|
24 | 31 | import java.util.List;
|
25 | 32 | import java.util.Locale;
|
@@ -585,4 +592,278 @@ private void closeOpenedStreams(Stack<Closeable> streams) throws IOException {
|
585 | 592 | }
|
586 | 593 | }
|
587 | 594 | }
|
| 595 | + |
| 596 | + public static class File { |
| 597 | + |
| 598 | + public static void readText(final String path, final String encoding, final CompleteCallback callback, final Object context) { |
| 599 | + final android.os.Handler mHandler = new android.os.Handler(); |
| 600 | + threadPoolExecutor().execute(new Runnable() { |
| 601 | + @Override |
| 602 | + public void run() { |
| 603 | + final ReadTextTask task = new ReadTextTask(callback, context); |
| 604 | + final String result = task.doInBackground(path, encoding); |
| 605 | + mHandler.post(new Runnable() { |
| 606 | + @Override |
| 607 | + public void run() { |
| 608 | + task.onPostExecute(result); |
| 609 | + } |
| 610 | + }); |
| 611 | + } |
| 612 | + }); |
| 613 | + } |
| 614 | + |
| 615 | + public static void read(final String path, final CompleteCallback callback, final Object context) { |
| 616 | + final android.os.Handler mHandler = new android.os.Handler(); |
| 617 | + threadPoolExecutor().execute(new Runnable() { |
| 618 | + @Override |
| 619 | + public void run() { |
| 620 | + final ReadTask task = new ReadTask(callback, context); |
| 621 | + final byte[] result = task.doInBackground(path); |
| 622 | + mHandler.post(new Runnable() { |
| 623 | + @Override |
| 624 | + public void run() { |
| 625 | + task.onPostExecute(result); |
| 626 | + } |
| 627 | + }); |
| 628 | + } |
| 629 | + }); |
| 630 | + } |
| 631 | + |
| 632 | + public static void writeText(final String path, final String content, final String encoding, final CompleteCallback callback, final Object context) { |
| 633 | + final android.os.Handler mHandler = new android.os.Handler(); |
| 634 | + threadPoolExecutor().execute(new Runnable() { |
| 635 | + @Override |
| 636 | + public void run() { |
| 637 | + final WriteTextTask task = new WriteTextTask(callback, context); |
| 638 | + final boolean result = task.doInBackground(path, content, encoding); |
| 639 | + mHandler.post(new Runnable() { |
| 640 | + @Override |
| 641 | + public void run() { |
| 642 | + task.onPostExecute(result); |
| 643 | + } |
| 644 | + }); |
| 645 | + } |
| 646 | + }); |
| 647 | + } |
| 648 | + |
| 649 | + public static void write(final String path, final byte[] content, final CompleteCallback callback, final Object context) { |
| 650 | + final android.os.Handler mHandler = new android.os.Handler(); |
| 651 | + threadPoolExecutor().execute(new Runnable() { |
| 652 | + @Override |
| 653 | + public void run() { |
| 654 | + final WriteTask task = new WriteTask(callback, context); |
| 655 | + final boolean result = task.doInBackground(path, content); |
| 656 | + mHandler.post(new Runnable() { |
| 657 | + @Override |
| 658 | + public void run() { |
| 659 | + task.onPostExecute(result); |
| 660 | + } |
| 661 | + }); |
| 662 | + } |
| 663 | + }); |
| 664 | + } |
| 665 | + |
| 666 | + static class ReadTextTask { |
| 667 | + private CompleteCallback callback; |
| 668 | + private Object context; |
| 669 | + |
| 670 | + public ReadTextTask(CompleteCallback callback, Object context) { |
| 671 | + this.callback = callback; |
| 672 | + this.context = context; |
| 673 | + } |
| 674 | + |
| 675 | + protected String doInBackground(String... params) { |
| 676 | + java.io.File javaFile = new java.io.File(params[0]); |
| 677 | + FileInputStream stream = null; |
| 678 | + |
| 679 | + try { |
| 680 | + stream = new FileInputStream(javaFile); |
| 681 | + |
| 682 | + InputStreamReader reader = new InputStreamReader(stream, params[1]); |
| 683 | + |
| 684 | + CharBuffer buffer = CharBuffer.allocate(81920); |
| 685 | + StringBuilder sb = new StringBuilder(); |
| 686 | + |
| 687 | + while (reader.read(buffer) != -1) { |
| 688 | + buffer.flip(); |
| 689 | + sb.append(buffer); |
| 690 | + buffer.clear(); |
| 691 | + } |
| 692 | + |
| 693 | + reader.close(); |
| 694 | + |
| 695 | + return sb.toString(); |
| 696 | + } catch (FileNotFoundException e) { |
| 697 | + Log.e(TAG, "Failed to read file, FileNotFoundException: " + e.getMessage()); |
| 698 | + return null; |
| 699 | + } catch (UnsupportedEncodingException e) { |
| 700 | + Log.e(TAG, "Failed to read file, UnsupportedEncodingException: " + e.getMessage()); |
| 701 | + return null; |
| 702 | + } catch (IOException e) { |
| 703 | + Log.e(TAG, "Failed to read file, IOException: " + e.getMessage()); |
| 704 | + return null; |
| 705 | + } finally { |
| 706 | + if (stream != null) { |
| 707 | + try { |
| 708 | + stream.close(); |
| 709 | + } catch (IOException e) { |
| 710 | + Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage()); |
| 711 | + } |
| 712 | + } |
| 713 | + } |
| 714 | + } |
| 715 | + |
| 716 | + protected void onPostExecute(final String result) { |
| 717 | + if (result != null) { |
| 718 | + this.callback.onComplete(result, this.context); |
| 719 | + } else { |
| 720 | + this.callback.onError("ReadTextTask returns no result.", this.context); |
| 721 | + } |
| 722 | + } |
| 723 | + } |
| 724 | + |
| 725 | + static class ReadTask { |
| 726 | + private CompleteCallback callback; |
| 727 | + private Object context; |
| 728 | + |
| 729 | + public ReadTask(CompleteCallback callback, Object context) { |
| 730 | + this.callback = callback; |
| 731 | + this.context = context; |
| 732 | + } |
| 733 | + |
| 734 | + protected byte[] doInBackground(String... params) { |
| 735 | + java.io.File javaFile = new java.io.File(params[0]); |
| 736 | + FileInputStream stream = null; |
| 737 | + |
| 738 | + try { |
| 739 | + stream = new FileInputStream(javaFile); |
| 740 | + |
| 741 | + byte[] result = new byte[(int)javaFile.length()]; |
| 742 | + |
| 743 | + DataInputStream dataInputStream = new DataInputStream(stream); |
| 744 | + dataInputStream.readFully(result); |
| 745 | + |
| 746 | + return result; |
| 747 | + } catch (FileNotFoundException e) { |
| 748 | + Log.e(TAG, "Failed to read file, FileNotFoundException: " + e.getMessage()); |
| 749 | + return null; |
| 750 | + } catch (IOException e) { |
| 751 | + Log.e(TAG, "Failed to read file, IOException: " + e.getMessage()); |
| 752 | + return null; |
| 753 | + } finally { |
| 754 | + if (stream != null) { |
| 755 | + try { |
| 756 | + stream.close(); |
| 757 | + } catch (IOException e) { |
| 758 | + Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage()); |
| 759 | + } |
| 760 | + } |
| 761 | + } |
| 762 | + } |
| 763 | + |
| 764 | + protected void onPostExecute(final byte[] result) { |
| 765 | + if (result != null) { |
| 766 | + this.callback.onComplete(result, this.context); |
| 767 | + } else { |
| 768 | + this.callback.onError("ReadTask returns no result.", this.context); |
| 769 | + } |
| 770 | + } |
| 771 | + } |
| 772 | + |
| 773 | + static class WriteTextTask { |
| 774 | + private CompleteCallback callback; |
| 775 | + private Object context; |
| 776 | + |
| 777 | + public WriteTextTask(CompleteCallback callback, Object context) { |
| 778 | + this.callback = callback; |
| 779 | + this.context = context; |
| 780 | + } |
| 781 | + |
| 782 | + protected boolean doInBackground(String... params) { |
| 783 | + java.io.File javaFile = new java.io.File(params[0]); |
| 784 | + FileOutputStream stream = null; |
| 785 | + try { |
| 786 | + stream = new FileOutputStream(javaFile); |
| 787 | + |
| 788 | + OutputStreamWriter writer = new OutputStreamWriter(stream, params[2]); |
| 789 | + |
| 790 | + writer.write(params[1]); |
| 791 | + writer.close(); |
| 792 | + |
| 793 | + return true; |
| 794 | + } catch (FileNotFoundException e) { |
| 795 | + Log.e(TAG, "Failed to write file, FileNotFoundException: " + e.getMessage()); |
| 796 | + return false; |
| 797 | + } catch (UnsupportedEncodingException e) { |
| 798 | + Log.e(TAG, "Failed to write file, UnsupportedEncodingException: " + e.getMessage()); |
| 799 | + return false; |
| 800 | + } catch (IOException e) { |
| 801 | + Log.e(TAG, "Failed to write file, IOException: " + e.getMessage()); |
| 802 | + return false; |
| 803 | + } finally { |
| 804 | + if (stream != null) { |
| 805 | + try { |
| 806 | + stream.close(); |
| 807 | + } catch (IOException e) { |
| 808 | + Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage()); |
| 809 | + } |
| 810 | + } |
| 811 | + } |
| 812 | + } |
| 813 | + |
| 814 | + protected void onPostExecute(final boolean result) { |
| 815 | + if (result) { |
| 816 | + this.callback.onComplete(null, this.context); |
| 817 | + } else { |
| 818 | + this.callback.onError("WriteTextTask returns no result.", this.context); |
| 819 | + } |
| 820 | + } |
| 821 | + } |
| 822 | + |
| 823 | + static class WriteTask { |
| 824 | + private CompleteCallback callback; |
| 825 | + private Object context; |
| 826 | + |
| 827 | + public WriteTask(CompleteCallback callback, Object context) { |
| 828 | + this.callback = callback; |
| 829 | + this.context = context; |
| 830 | + } |
| 831 | + |
| 832 | + protected boolean doInBackground(Object... params) { |
| 833 | + java.io.File javaFile = new java.io.File((String)params[0]); |
| 834 | + FileOutputStream stream = null; |
| 835 | + byte[] content = (byte[])params[1]; |
| 836 | + |
| 837 | + try { |
| 838 | + stream = new FileOutputStream(javaFile); |
| 839 | + stream.write(content, 0, content.length); |
| 840 | + |
| 841 | + return true; |
| 842 | + } catch (FileNotFoundException e) { |
| 843 | + Log.e(TAG, "Failed to write file, FileNotFoundException: " + e.getMessage()); |
| 844 | + return false; |
| 845 | + } catch (IOException e) { |
| 846 | + Log.e(TAG, "Failed to write file, IOException: " + e.getMessage()); |
| 847 | + return false; |
| 848 | + } finally { |
| 849 | + if (stream != null) { |
| 850 | + try { |
| 851 | + stream.close(); |
| 852 | + } catch (IOException e) { |
| 853 | + Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage()); |
| 854 | + } |
| 855 | + } |
| 856 | + } |
| 857 | + } |
| 858 | + |
| 859 | + protected void onPostExecute(final boolean result) { |
| 860 | + if (result) { |
| 861 | + this.callback.onComplete(null, this.context); |
| 862 | + } else { |
| 863 | + this.callback.onError("WriteTask returns no result.", this.context); |
| 864 | + } |
| 865 | + } |
| 866 | + } |
| 867 | + |
| 868 | + } |
588 | 869 | }
|
0 commit comments