14
14
15
15
package com .google .firebase .crashlytics .internal .common ;
16
16
17
+ import android .content .Context ;
17
18
import androidx .annotation .NonNull ;
18
19
import androidx .annotation .Nullable ;
19
- import com .google .firebase .crashlytics .core .MetaDataStore ;
20
20
import com .google .firebase .crashlytics .internal .CrashlyticsNativeComponent ;
21
21
import com .google .firebase .crashlytics .internal .Logger ;
22
22
import com .google .firebase .crashlytics .internal .NativeSessionFileProvider ;
23
- import com .google .firebase .crashlytics .internal .log .LogFileManager ;
24
23
import com .google .firebase .crashlytics .internal .ndk .NativeFileUtils ;
25
- import com . google . firebase . crashlytics . internal . persistence . FileStore ;
24
+ import java . io . ByteArrayInputStream ;
26
25
import java .io .File ;
27
26
import java .io .FileInputStream ;
28
27
import java .io .FileOutputStream ;
29
28
import java .io .IOException ;
29
+ import java .io .InputStream ;
30
30
import java .util .zip .GZIPOutputStream ;
31
31
32
- class GzipFileNativeSessionHandler implements NativeComponentSessionHandler <Void > {
32
+ class GzipFileNativeSessionProcessingStrategy implements NativeSessionProcessingStrategy <Void > {
33
33
34
- private CrashlyticsNativeComponent nativeComponent ;
35
- private FileStore fileStore ;
34
+ interface OutputDirectoryProvider {
35
+ File getOutputDirectory (String sessionId );
36
+ }
37
+
38
+ private final Context context ;
39
+ private final OutputDirectoryProvider outputDirectoryProvider ;
40
+
41
+ public GzipFileNativeSessionProcessingStrategy (
42
+ Context context , OutputDirectoryProvider outputDirectoryProvider ) {
43
+ this .context = context ;
44
+ this .outputDirectoryProvider = outputDirectoryProvider ;
45
+ }
36
46
37
47
@ Override
38
- public Void handlePreviousNativeSession (String sessionId ) throws IOException {
48
+ public Void processNativeSession (
49
+ CrashlyticsNativeComponent nativeComponent ,
50
+ String sessionId ,
51
+ InputStream keysInput ,
52
+ InputStream logsInput ,
53
+ InputStream userInput )
54
+ throws IOException {
39
55
Logger .getLogger ().d ("Finalizing native report for session " + sessionId );
40
56
NativeSessionFileProvider nativeSessionFileProvider =
41
57
nativeComponent .getSessionFileProvider (sessionId );
@@ -53,74 +69,71 @@ public Void handlePreviousNativeSession(String sessionId) throws IOException {
53
69
return null ;
54
70
}
55
71
56
- final File filesDir = fileStore .getFilesDir ();
57
- final MetaDataStore metaDataStore = new MetaDataStore (filesDir );
58
- final File sessionUser = metaDataStore .getUserDataFileForSession (sessionId );
59
- final File sessionKeys = metaDataStore .getKeysFileForSession (sessionId );
60
-
61
- final LogFileManager previousSessionLogManager =
62
- new LogFileManager (getContext (), logFileDirectoryProvider , sessionId );
63
- final byte [] logs = previousSessionLogManager .getBytesForLog ();
64
-
65
- final File nativeSessionDirectory = new File (getNativeSessionFilesDir (), sessionId );
72
+ final File nativeSessionDirectory = outputDirectoryProvider .getOutputDirectory (sessionId );
66
73
67
74
if (!nativeSessionDirectory .mkdirs ()) {
68
75
Logger .getLogger ().d ("Couldn't create native sessions directory" );
69
- return ;
76
+ return null ;
70
77
}
71
78
72
79
gzipFile (minidump , new File (nativeSessionDirectory , "minidump" ));
73
- gzipIfNotEmpty (
80
+ gzipBytes (
74
81
NativeFileUtils .binaryImagesJsonFromMapsFile (binaryImages , context ),
75
82
new File (nativeSessionDirectory , "binaryImages" ));
76
83
gzipFile (metadata , new File (nativeSessionDirectory , "metadata" ));
77
84
gzipFile (sessionFile , new File (nativeSessionDirectory , "session" ));
78
85
gzipFile (sessionApp , new File (nativeSessionDirectory , "app" ));
79
86
gzipFile (sessionDevice , new File (nativeSessionDirectory , "device" ));
80
87
gzipFile (sessionOs , new File (nativeSessionDirectory , "os" ));
81
- gzipFile ( sessionUser , new File (nativeSessionDirectory , "user" ));
82
- gzipFile ( sessionKeys , new File (nativeSessionDirectory , "keys" ));
83
- gzipIfNotEmpty ( logs , new File (nativeSessionDirectory , "logs" ));
88
+ gzipInputStream ( userInput , new File (nativeSessionDirectory , "user" ));
89
+ gzipInputStream ( keysInput , new File (nativeSessionDirectory , "keys" ));
90
+ gzipInputStream ( logsInput , new File (nativeSessionDirectory , "logs" ));
84
91
85
92
return null ;
86
93
}
87
94
88
- private static void gzipFile (@ NonNull File input , @ NonNull File output ) throws IOException {
89
- if (!input .exists () || !input .isFile ()) {
95
+ private static void gzipBytes (@ Nullable byte [] bytes , @ NonNull File outputFile )
96
+ throws IOException {
97
+ if (bytes == null || bytes .length == 0 ) {
98
+ return ;
99
+ }
100
+ ByteArrayInputStream bis = new ByteArrayInputStream (bytes );
101
+ try {
102
+ gzipInputStream (bis , outputFile );
103
+ } finally {
104
+ CommonUtils .closeQuietly (bis );
105
+ }
106
+ }
107
+
108
+ private static void gzipFile (@ Nullable File input , @ NonNull File outputFile ) throws IOException {
109
+ if (input == null ) {
90
110
return ;
91
111
}
92
- byte [] buffer = new byte [1024 ];
93
112
FileInputStream fis = null ;
94
- GZIPOutputStream gos = null ;
95
113
try {
96
114
fis = new FileInputStream (input );
97
- gos = new GZIPOutputStream (new FileOutputStream (output ));
98
-
99
- int read ;
100
-
101
- while ((read = fis .read (buffer )) > 0 ) {
102
- gos .write (buffer , 0 , read );
103
- }
104
-
105
- gos .finish ();
115
+ gzipInputStream (fis , outputFile );
106
116
} finally {
107
117
CommonUtils .closeQuietly (fis );
108
- CommonUtils .closeQuietly (gos );
109
118
}
110
119
}
111
120
112
- private static void gzipIfNotEmpty (@ Nullable byte [] content , @ NonNull File path )
121
+ private static void gzipInputStream (@ Nullable InputStream input , @ NonNull File output )
113
122
throws IOException {
114
- if (content != null && content . length > 0 ) {
115
- gzip ( content , path ) ;
123
+ if (input == null ) {
124
+ return ;
116
125
}
117
- }
118
-
119
- private static void gzip (@ NonNull byte [] bytes , @ NonNull File path ) throws IOException {
126
+ byte [] buffer = new byte [1024 ];
120
127
GZIPOutputStream gos = null ;
121
128
try {
122
- gos = new GZIPOutputStream (new FileOutputStream (path ));
123
- gos .write (bytes , 0 , bytes .length );
129
+ gos = new GZIPOutputStream (new FileOutputStream (output ));
130
+
131
+ int read ;
132
+
133
+ while ((read = input .read (buffer )) > 0 ) {
134
+ gos .write (buffer , 0 , read );
135
+ }
136
+
124
137
gos .finish ();
125
138
} finally {
126
139
CommonUtils .closeQuietly (gos );
0 commit comments