##### Coverage Report - com.ctrip.framework.apollo.core.utils.PropertiesUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
PropertiesUtil |
0% | 0/13
0% | 0/4
| 3.0;3

1 |  |
     package com.ctrip.framework.apollo.core.utils;
2 |  |
3 |  |
     import java.io.IOException;
4 |  |
     import java.io.StringWriter;
5 |  |
     import java.util.Properties;
6 |  |
7 |  |
     /**
8 |  |
      * @author Jason Song(song_s@ctrip.com)
9 |  |
      */
10 | 0 |
     public class PropertiesUtil {
11 |  |
       /**
12 |  |
        * Transform the properties to string format
13 |  |
        * @param properties the properties object
14 |  |
        * @return the string containing the properties
15 |  |
        * @throws IOException
16 |  |
        */
17 |  |
       public static String toString(Properties properties) throws IOException {
18 | 0 |
         StringWriter writer = new StringWriter();
19 | 0 |
         properties.store(writer, null);
20 | 0 |
         StringBuffer stringBuffer = writer.getBuffer();
21 | 0 |
         filterPropertiesComment(stringBuffer);
22 | 0 |
         return stringBuffer.toString();
23 |  |
       }
24 |  |
25 |  |
       /**
26 |  |
        * filter out the first comment line
27 |  |
        * @param stringBuffer the string buffer
28 |  |
        * @return true if filtered successfully, false otherwise
29 |  |
        */
30 |  |
       static boolean filterPropertiesComment(StringBuffer stringBuffer) {
31 |  |
         //check whether has comment in the first line
32 | 0 |
         if (stringBuffer.charAt(0) != '#') {
33 | 0 |
           return false;
34 |  |
         }
35 | 0 |
         int commentLineIndex = stringBuffer.indexOf("\n");
36 | 0 |
         if (commentLineIndex == -1) {
37 | 0 |
           return false;
38 |  |
         }
39 | 0 |
         stringBuffer.delete(0, commentLineIndex + 1);
40 | 0 |
         return true;
41 |  |
       }
42 |  |
     }