1
+ /*
2
+ * AssetFileTransfer.java
3
+ * artoolkitX
4
+ *
5
+ * This file is part of artoolkitX.
6
+ *
7
+ * artoolkitX is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * artoolkitX is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
19
+ *
20
+ * As a special exception, the copyright holders of this library give you
21
+ * permission to link this library with independent modules to produce an
22
+ * executable, regardless of the license terms of these independent modules, and to
23
+ * copy and distribute the resulting executable under terms of your choice,
24
+ * provided that you also meet, for each linked independent module, the terms and
25
+ * conditions of the license of that module. An independent module is a module
26
+ * which is neither derived from nor based on this library. If you modify this
27
+ * library, you may extend this exception to your version of the library, but you
28
+ * are not obligated to do so. If you do not wish to do so, delete this exception
29
+ * statement from your version.
30
+ *
31
+ * Copyright 2018 Realmax, Inc.
32
+ * Copyright 2015 Daqri, LLC.
33
+ * Copyright 2011-2015 ARToolworks, Inc.
34
+ *
35
+ * Author(s): Julian Looser, Philip Lamb
36
+ *
37
+ */
38
+
39
+ package org .artoolkitx .arx .arxj .assets ;
40
+
41
+ import android .content .res .AssetManager ;
42
+ import android .os .Environment ;
43
+ import android .util .Log ;
44
+
45
+ import java .io .File ;
46
+ import java .io .FileNotFoundException ;
47
+ import java .io .FileOutputStream ;
48
+ import java .io .IOException ;
49
+ import java .io .InputStream ;
50
+ import java .io .OutputStream ;
51
+
52
+ class AssetFileTransfer {
53
+
54
+ private static final String TAG = "AssetFileTransfer" ;
55
+
56
+ private File assetFile ;
57
+ private boolean assetAvailable ;
58
+
59
+ public File targetFile ;
60
+ private File targetDirectory ;
61
+
62
+ private boolean targetFileAlreadyExists ;
63
+ private long targetFileCRC ;
64
+ private File tempFile ;
65
+ private long tempFileCRC ;
66
+
67
+
68
+ private boolean
69
+ assetCopied ;
70
+
71
+ private void copyContents (InputStream in , OutputStream out ) throws IOException {
72
+
73
+ final int bufferSize = 16384 ;
74
+ byte [] buffer = new byte [bufferSize ];
75
+
76
+ int bytesRead ;
77
+ while ((bytesRead = in .read (buffer )) != -1 ) {
78
+ out .write (buffer , 0 , bytesRead );
79
+ }
80
+
81
+ out .flush ();
82
+ }
83
+
84
+
85
+ public void copyAssetToTargetDir (AssetManager manager , String assetFilePath , String targetDirPath ) throws AssetFileTransferException {
86
+
87
+ assetFile = new File (assetFilePath );
88
+
89
+ InputStream in ;
90
+ OutputStream out ;
91
+
92
+ try {
93
+ in = manager .open (assetFilePath );
94
+ assetAvailable = true ;
95
+ } catch (IOException e ) {
96
+ assetAvailable = false ;
97
+ throw new AssetFileTransferException ("Unable to open the asset file: " + assetFilePath , e );
98
+ }
99
+
100
+ targetFile = new File (targetDirPath , assetFilePath );
101
+ targetFileAlreadyExists = targetFile .exists ();
102
+
103
+ Log .i (TAG , "copyAssetToTargetDir(): [" + assetFilePath + "] -> [" + targetFile .getPath () + "]" );
104
+
105
+ if (targetFileAlreadyExists ) {
106
+
107
+ //Log.i(TAG, "Target file exists. Unpacking to temporary file first.");
108
+
109
+ // Create temporary file to unpack to
110
+ try {
111
+ tempFile = File .createTempFile ("unpacker" , null , Environment .getExternalStorageDirectory ());
112
+ //Log.i(TAG, "Created temp file for unpacking: " + tempFile.getPath());
113
+ } catch (IOException ioe ) {
114
+ throw new AssetFileTransferException ("Error creating temp file: " + tempFile .getPath (), ioe );
115
+ }
116
+
117
+ // Copy asset to temporary file
118
+ try {
119
+ out = new FileOutputStream (tempFile );
120
+ } catch (FileNotFoundException fnfe ) {
121
+ throw new AssetFileTransferException ("Error creating temp file: " + tempFile .getPath (), fnfe );
122
+ }
123
+ try {
124
+ copyContents (in , out );
125
+ in .close ();
126
+ in = null ;
127
+ out .close ();
128
+ out = null ;
129
+ } catch (IOException ioe ) {
130
+ throw new AssetFileTransferException ("Error copying asset to temp file: " + tempFile .getPath (), ioe );
131
+ }
132
+
133
+ // Get hashes for new temporary file and existing file
134
+ try {
135
+
136
+ //tempFileHash = Hasher.computeHash(tempFile.getPath());
137
+ tempFileCRC = Hasher .computeCRC (tempFile .getPath ());
138
+
139
+ //targetFileHash = Hasher.computeHash(targetFile.getPath());
140
+ targetFileCRC = Hasher .computeCRC (targetFile .getPath ());
141
+
142
+ } catch (HashComputationException hce ) {
143
+ throw new AssetFileTransferException ("Error hashing files" , hce );
144
+ }
145
+
146
+ if (tempFileCRC == targetFileCRC ) {
147
+
148
+ // The hashes match. The files are the same, so don't need to do anything.
149
+ //Log.i(TAG, "The hashes match. Keeping existing file, removing temp file.");
150
+ // Clean up temporary file
151
+ tempFile .delete ();
152
+
153
+ } else {
154
+
155
+ // The hashes do not match. Overwrite the existing file with the new one.
156
+ targetFile .delete ();
157
+ //Log.i(TAG, "Deleted existing file");
158
+ tempFile .renameTo (targetFile );
159
+ //Log.i(TAG, "Moved temp file: " + tempFile.getPath() + " to " + targetFile.getPath());
160
+ assetCopied = true ;
161
+ }
162
+
163
+ } else {
164
+
165
+ Log .i (TAG , "copyAssetToTargetDir(): Target file does not exist. Creating directory structure." );
166
+
167
+ // Ensure parent directories exist so we can create the file
168
+ targetDirectory = targetFile .getParentFile ();
169
+ targetDirectory .mkdirs ();
170
+
171
+ // Copy asset to target file
172
+ try {
173
+ out = new FileOutputStream (targetFile );
174
+ } catch (FileNotFoundException fnfe ) {
175
+ throw new AssetFileTransferException ("Error creating target file: " + targetFile .getPath (), fnfe );
176
+ }
177
+ try {
178
+ copyContents (in , new FileOutputStream (targetFile ));
179
+ //Log.i(TAG, "Copied asset to target file");
180
+
181
+ in .close ();
182
+ in = null ;
183
+ out .close ();
184
+ out = null ;
185
+ } catch (IOException ioe ) {
186
+ throw new AssetFileTransferException ("Error copying asset to target file: " + targetFile .getPath (), ioe );
187
+ }
188
+ assetCopied = true ;
189
+ }
190
+ }
191
+ }
0 commit comments