2222public class Base64CodecUtil {
2323 private static final Base64 base64 = new Base64 ();
2424
25- public static final String BASE64_START = "$BASE64$" ;
26- public static final String BASE64_END = "$" ;
2725 /**
2826 *
2927 * @param origin
@@ -58,74 +56,13 @@ public static String decode(String baseStr){
5856
5957
6058
61-
62- public static boolean isBase64encode (String base64Str ){
63- return base64Str .startsWith (BASE64_START )&&base64Str .endsWith (BASE64_END );
64- }
65-
66-
67-
68- /**
69- * IPFS 消息解码
70- * @param encodeStr
71- * @return
72- */
73- public static String decodeIPFSMsg (String encodeStr ){
74- if (isBase64encode (encodeStr )){
75- int len = encodeStr .length ();
76- encodeStr = encodeStr .substring (BASE64_START .length (),len -1 );
77- return decode (encodeStr );
78- }else {
79- return encodeStr ;
80- }
81- }
82-
83- /**
84- *
85- * @param message
86- * @return
87- * @throws IllegalIPFSMessageException
88- */
89- public PeerBoradcastInfo parseFromIm (IpfsMessage message ) throws IllegalIPFSMessageException {
90- if (message ==null
91- ||message .getTypes ()!=CtrlTypes .online
92- ||(StringUtils .isBlank (message .getContents ())&&StringUtils .isBlank (message .getData ())))
93- return null ;
94- if (StringUtils .isBlank (message .getContents ())){
95- String jsonData = decode (message .getData ());
96- if (!jsonData .startsWith (CtrlTypes .online .getPreffix ())||!jsonData .endsWith (CtrlTypes .online .sperator )){
97- throw new IllegalIPFSMessageException ("message data not online type message" );
98- }
99- int len = jsonData .length ();
100- message .setContents (jsonData .substring (CtrlTypes .online .getPreffixLength (),len -1 ));
101- }
102- PeerBoradcastInfo res = JSON .parseObject (message .getContents (),PeerBoradcastInfo .class );
103- return res ;
104- }
105-
106- /**
107- * 编码控制消息
108- * @param o
109- * @param type
110- * @return
111- */
112- public static String encodeCtrlMsg (Object o ,CtrlTypes type ){
113- if (o ==null )return null ;
114- if (type ==null )type =CtrlTypes .online ;
115- StringBuffer mSb = new StringBuffer ();
116- mSb .append (type .sperator ).append (type .starter ).append (type .sperator );
117- String json = JSON .toJSONString (o );
118- mSb .append (encode (json ));
119- mSb .append (type .sperator );
120- return mSb .toString ();
121- }
122-
12359 /**
12460 *
12561 * @param message
12662 * @param types
12763 * @return
12864 */
65+ @ Deprecated
12966 public static String encodeByCtrlType (Object message ,CtrlTypes types ){
13067 if (message ==null )return null ;
13168 StringBuffer sb = new StringBuffer ();
@@ -154,6 +91,7 @@ public static String encodeByCtrlType(Object message,CtrlTypes types){
15491 * @param m
15592 * @return
15693 */
94+ @ Deprecated
15795 public static IpfsMessage parseIpmsMessageCtrlType (IpfsMessage m ){
15896 if (m ==null ||m .getData ()==null ||m .getData ().length ()<1 )return null ;
15997 String decode64 = decode (m .getData ());
@@ -179,25 +117,9 @@ public static IpfsMessage parseIpmsMessageCtrlType(IpfsMessage m){
179117 }
180118
181119
182- /**
183- *
184- * @param baseStr
185- * @param type
186- * @return
187- */
188- public static String decodeCtrlMsg (String baseStr ,CtrlTypes type ){
189- if (baseStr ==null )return null ;
190- if (type ==null )type =CtrlTypes .online ;
191- if (baseStr .startsWith (type .sperator +type .starter +type .sperator )&&baseStr .endsWith (type .sperator +"" )){
192- int len = baseStr .length ();
193- baseStr = baseStr .substring (type .starter .length ()+2 ,len -1 );
194- return decode (baseStr );
195- }else {
196- return decode (baseStr );
197- }
198- }
199120
200121
122+ @ Deprecated
201123 public static enum CtrlTypes {
202124 /**
203125 * $ON.B64.J$xxxxxsssds$
@@ -263,24 +185,4 @@ public String toString() {
263185 }
264186 }
265187
266- public static void main (String [] args ) {
267- String in = "你好NBS 的圣诞 xccxv 节收到 \\ n slfsak阿卡恢复大师的/n" ;
268- System .out .println (in );
269- String encodeIn = encode (in );
270- System .out .println (encodeIn );
271- String enMsg = "$BASE64|" +encodeIn +"|" ;
272- System .out .println (enMsg );
273- String dec = decodeIPFSMsg (enMsg );
274- System .out .println (dec );
275- String h = "Hello!" ;
276- String res = "" ;
277- res = Base64CodecUtil .encode (h );
278- System .out .println (res );
279-
280-
281- Multihash multihash = Multihash .fromHex ("EiBCOcWflebcLfrIFDlcWTMFG+2sQ6dGv2BKlop/JjrRmA==" );
282-
283- System .out .println (Base64CodecUtil .decode ("EiBCOcWflebcLfrIFDlcWTMFG+2sQ6dGv2BKlop/JjrRmA==" ));
284-
285- }
286188}
0 commit comments