55import java .io .ByteArrayInputStream ;
66import java .io .ByteArrayOutputStream ;
77import java .io .DataInputStream ;
8+ import java .io .File ;
89import java .io .IOException ;
910import java .io .InputStream ;
1011import java .io .OutputStream ;
12+ import java .io .UnsupportedEncodingException ;
1113import java .net .ServerSocket ;
1214import java .net .Socket ;
1315import java .net .URLDecoder ;
16+ import java .nio .file .Files ;
17+ import java .nio .file .attribute .BasicFileAttributes ;
18+ import java .nio .file .attribute .FileTime ;
19+ import java .text .DateFormat ;
1420import java .util .ArrayList ;
1521import java .util .Collection ;
1622import java .util .Collections ;
@@ -55,32 +61,6 @@ public void run() {
5561 protected abstract void log (String message , Exception exception );
5662 protected abstract void log (String message );
5763
58- /**
59- * Appends a string field to the given JSON string
60- */
61- private void appendJson (StringBuilder json , String field , String value , boolean nextField ) {
62- json .append (field )
63- .append (":'" )
64- .append (value )
65- .append ("'" );
66- if (nextField ) {
67- json .append ("," );
68- }
69- }
70-
71- /**
72- * Appends an int field to the given JSON string
73- */
74- private void appendJson (StringBuilder json , String field , long value , boolean nextField ) {
75- json .append (field )
76- .append (":'" )
77- .append (value )
78- .append ("'" );
79- if (nextField ) {
80- json .append ("," );
81- }
82- }
83-
8464 /**
8565 * Parses HTTP GET parameters with the given name
8666 */
@@ -168,21 +148,23 @@ private Response handleDownload(Map<String, Collection<String>> parameters) thro
168148 * Handler for files API
169149 */
170150 private Response handleFileList () throws IOException {
171- StringBuilder json = new StringBuilder ("[" );
151+ JsonBuilder builder = new JsonBuilder ();
152+ builder .append ('[' );
172153 long id = 0 ;
173- String comma = "" ;
154+ char comma = 0 ;
174155 for (FileData nextFile : getFileData ()) {
175- json .append (comma );
176- json .append ("{" );
177- appendJson ( json , "id" , id ++, true );
178- appendJson ( json , "fileName" , nextFile .fileName , true );
179- appendJson ( json , "size" , nextFile .size , true );
180- appendJson ( json , "date" , nextFile .date , false );
181- json .append ("}" );
182- comma = "," ;
156+ builder .append (comma );
157+ builder .append ('{' );
158+ builder . append ( "id" , id ++, true );
159+ builder . append ( "fileName" , nextFile .fileName , true );
160+ builder . append ( "size" , nextFile .size , true );
161+ builder . append ( "date" , nextFile .date , false );
162+ builder .append ('}' );
163+ comma = ',' ;
183164 }
184- json .append ("]" );
185- return new Response (new ByteArrayInputStream (json .toString ().getBytes ("utf-8" )), json .length ());
165+ builder .append (']' );
166+ byte [] json = builder .getBytes ();
167+ return new Response (new ByteArrayInputStream (json ), json .length );
186168 }
187169
188170 /**
@@ -298,6 +280,56 @@ public static class FileData {
298280 String fileName ;
299281 String date ;
300282 long size ;
283+
284+ FileData (File file ) throws IOException {
285+ BasicFileAttributes attr = Files .readAttributes (file .toPath (), BasicFileAttributes .class );
286+ FileTime lastModifiedTime = attr .lastModifiedTime ();
287+ DateFormat dateFormat = DateFormat .getDateInstance (DateFormat .DEFAULT );
288+ this .fileName = file .getName ();
289+ this .date = dateFormat .format (lastModifiedTime .toMillis ());
290+ this .size = file .length ();
291+ }
292+ }
293+
294+ /**
295+ * Build JSON string
296+ */
297+ public static class JsonBuilder {
298+ private final StringBuilder json ;
299+
300+ JsonBuilder () {
301+ json = new StringBuilder ();
302+ }
303+
304+ void append (char chr ) {
305+ if (chr > 0 ) {
306+ json .append (chr );
307+ }
308+ }
309+
310+ void append (String field , String value , boolean nextField , boolean quote ) {
311+ json .append ("\" " )
312+ .append (field )
313+ .append ("\" :" )
314+ .append (quote ? "\" " : "" )
315+ .append (value )
316+ .append (quote ? "\" " : "" );
317+ if (nextField ) {
318+ json .append ("," );
319+ }
320+ }
321+
322+ void append (String field , String value , boolean nextField ) {
323+ this .append (field , value , nextField , true );
324+ }
325+
326+ void append (String field , long value , boolean nextField ) {
327+ this .append (field , String .valueOf (value ), nextField , false );
328+ }
329+
330+ byte [] getBytes () throws UnsupportedEncodingException {
331+ return json .toString ().getBytes ("utf-8" );
332+ }
301333 }
302334
303335 /**
@@ -312,11 +344,11 @@ public Response(InputStream inputStream, long length) {
312344 this .length = length ;
313345 }
314346
315- public InputStream getInputStream () {
347+ InputStream getInputStream () {
316348 return inputStream ;
317349 }
318350
319- public long getLength () {
351+ long getLength () {
320352 return length ;
321353 }
322354
0 commit comments