4040import java .io .BufferedReader ;
4141import java .io .ByteArrayOutputStream ;
4242import java .io .File ;
43+ import java .io .FileInputStream ;
4344import java .io .FileNotFoundException ;
4445import java .io .IOException ;
4546import java .io .InputStream ;
4647import java .io .InputStreamReader ;
48+ import java .io .OutputStream ;
4749import java .io .PrintStream ;
4850import java .net .ServerSocket ;
4951import java .net .Socket ;
@@ -84,6 +86,11 @@ public class ClientServer implements Runnable {
8486 private Gson mGson ;
8587 private boolean isDbOpenned ;
8688
89+ /**
90+ * Hold the selected database name
91+ */
92+ private String mSelectedDatabase = null ;
93+
8794 /**
8895 * WebServer constructor.
8996 */
@@ -226,13 +233,17 @@ private void handle(Socket socket) throws IOException {
226233 if (Constants .APP_SHARED_PREFERENCES .equals (database )) {
227234 response = getAllPrefTableName ();
228235 closeDatabase ();
236+ mSelectedDatabase = null ;
229237 } else {
230238 openDatabase (database );
231239 response = getAllTableName ();
240+ mSelectedDatabase = database ;
232241 }
233242
234243 String data = mGson .toJson (response );
235244 bytes = data .getBytes ();
245+ } else if (route .startsWith ("downloadDb" )) {
246+ bytes = getDatabaseFile ();
236247 } else {
237248 bytes = loadContent (route );
238249 }
@@ -246,7 +257,12 @@ private void handle(Socket socket) throws IOException {
246257 // Send out the content.
247258 output .println ("HTTP/1.0 200 OK" );
248259 output .println ("Content-Type: " + detectMimeType (route ));
249- output .println ("Content-Length: " + bytes .length );
260+
261+ if (route .startsWith ("downloadDb" )) {
262+ output .println ("Content-Disposition: attachment; filename=" + mSelectedDatabase );
263+ } else {
264+ output .println ("Content-Length: " + bytes .length );
265+ }
250266 output .println ();
251267 output .write (bytes );
252268 output .flush ();
@@ -318,6 +334,32 @@ private String detectMimeType(String fileName) {
318334 }
319335 }
320336
337+ private byte [] getDatabaseFile () {
338+ if (TextUtils .isEmpty (mSelectedDatabase )) {
339+ return null ;
340+ }
341+
342+ File file = new File (mDatabaseDir , mSelectedDatabase );
343+
344+ byte [] byteArray = null ;
345+ try {
346+ InputStream inputStream = new FileInputStream (file );
347+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
348+ byte [] b = new byte [(int ) file .length ()];
349+ int bytesRead = 0 ;
350+
351+ while ((bytesRead = inputStream .read (b )) != -1 ) {
352+ bos .write (b , 0 , bytesRead );
353+ }
354+
355+ byteArray = bos .toByteArray ();
356+ } catch (IOException e ) {
357+ Log .e (TAG , "getDatabaseFile: " , e );
358+ }
359+
360+ return byteArray ;
361+ }
362+
321363 private void getDatabaseDir () {
322364 File root = mContext .getFilesDir ().getParentFile ();
323365 File dbRoot = new File (root , "/databases" );
@@ -383,7 +425,7 @@ private Response query(String sql) {
383425 row .add (cursor .getFloat (i ));
384426 break ;
385427 case Cursor .FIELD_TYPE_INTEGER :
386- row .add (cursor .getInt (i ));
428+ row .add (cursor .getLong (i ));
387429 break ;
388430 case Cursor .FIELD_TYPE_STRING :
389431 row .add (cursor .getString (i ));
0 commit comments