| 1 | import java.io.BufferedReader; |
|---|
| 2 | import java.io.BufferedWriter; |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.FileOutputStream; |
|---|
| 5 | import java.io.FileReader; |
|---|
| 6 | import java.io.FileWriter; |
|---|
| 7 | import java.io.IOException; |
|---|
| 8 | import java.io.RandomAccessFile; |
|---|
| 9 | import java.text.ParsePosition; |
|---|
| 10 | import java.text.SimpleDateFormat; |
|---|
| 11 | import java.util.Date; |
|---|
| 12 | import java.util.Locale; |
|---|
| 13 | import java.util.StringTokenizer; |
|---|
| 14 | import java.util.regex.Matcher; |
|---|
| 15 | import java.util.regex.Pattern; |
|---|
| 16 | |
|---|
| 17 | import org.apache.hadoop.fs.FileStatus; |
|---|
| 18 | import org.apache.hadoop.fs.FileSystem; |
|---|
| 19 | import org.apache.hadoop.fs.Path; |
|---|
| 20 | import org.apache.hadoop.mapred.JobConf; |
|---|
| 21 | |
|---|
| 22 | // this example is testing for static valiable value |
|---|
| 23 | // it will show the value of static valiable is related to Class, not object |
|---|
| 24 | |
|---|
| 25 | class testb { |
|---|
| 26 | // object variable |
|---|
| 27 | public void read() { |
|---|
| 28 | System.out.println(test.sna); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | class Fu<T> { |
|---|
| 33 | T go; |
|---|
| 34 | |
|---|
| 35 | void setFu(T go) { |
|---|
| 36 | this.go = go; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | T getFu() { |
|---|
| 40 | return go; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | class animal { |
|---|
| 45 | String head = "head"; |
|---|
| 46 | |
|---|
| 47 | String feet = "feet"; |
|---|
| 48 | |
|---|
| 49 | String body = "body"; |
|---|
| 50 | |
|---|
| 51 | String hand = "head"; |
|---|
| 52 | |
|---|
| 53 | String mind = "mind"; |
|---|
| 54 | |
|---|
| 55 | int id = 1234567; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | class bird extends animal { |
|---|
| 59 | String feather = "feather"; |
|---|
| 60 | |
|---|
| 61 | bird() { |
|---|
| 62 | hand = "wing"; |
|---|
| 63 | id = 1234568; |
|---|
| 64 | System.out.println(head + hand + body + feet + "id=" + id); |
|---|
| 65 | System.out.println("super = " + super.hand + "this = " + this.hand); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | class dog extends animal { |
|---|
| 70 | dog(animal a) { |
|---|
| 71 | System.out.println(a.head + hand + body + feet + "id=" + id); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void setbird(bird b) { |
|---|
| 75 | System.out.println(b.head + hand + body + feet + "id=" + id); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | public class test { |
|---|
| 80 | int na; |
|---|
| 81 | |
|---|
| 82 | static int sna; |
|---|
| 83 | |
|---|
| 84 | static String str; |
|---|
| 85 | |
|---|
| 86 | public test() { |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public test(String st) { |
|---|
| 90 | str = st; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public test(int a, int b, String st) { |
|---|
| 94 | na = a; |
|---|
| 95 | sna = b; |
|---|
| 96 | str = st; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public void print(String str) { |
|---|
| 100 | System.out.println(str); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | @SuppressWarnings( { "static-access" }) |
|---|
| 104 | public void strToken() throws IOException { |
|---|
| 105 | test a = new test(1, 2, "a"); |
|---|
| 106 | a.print("a.na = " + a.na); |
|---|
| 107 | a.print("test.sna = " + test.sna); |
|---|
| 108 | |
|---|
| 109 | a.print("a.sna = " + a.sna); // warning about it's not a regular |
|---|
| 110 | // accessing method |
|---|
| 111 | |
|---|
| 112 | test b = new test(3, 4, "a"); |
|---|
| 113 | b.print("b.na = " + b.na); |
|---|
| 114 | b.print("test.sna = " + test.sna); |
|---|
| 115 | b.print("b.sna = " + b.sna); // warning about it's not a regular |
|---|
| 116 | // accessing method |
|---|
| 117 | |
|---|
| 118 | a.print("a.sna = " + a.sna); // a.sna = test.sna -> b.sna |
|---|
| 119 | String tmp[] = test.str.split(":"); |
|---|
| 120 | String Column_Family = tmp[0] + ":"; |
|---|
| 121 | a.print("column family = " + Column_Family); |
|---|
| 122 | a.print("ori str = " + test.str); |
|---|
| 123 | // test fileoutputstream |
|---|
| 124 | FileOutputStream out = new FileOutputStream(new File( |
|---|
| 125 | "/home/waue/mr-result.txt")); |
|---|
| 126 | out.write("hello".getBytes()); |
|---|
| 127 | out.close(); |
|---|
| 128 | // test randomaccessfile |
|---|
| 129 | RandomAccessFile raf = new RandomAccessFile("/home/waue/mr-result.txt", |
|---|
| 130 | "rw"); |
|---|
| 131 | raf.seek(raf.length()); // move pointer to end |
|---|
| 132 | raf.write("\n go \t ahead".getBytes()); |
|---|
| 133 | raf.close(); |
|---|
| 134 | // test StringTokenizer |
|---|
| 135 | StringTokenizer st = new StringTokenizer("this is a test"); |
|---|
| 136 | while (st.hasMoreTokens()) { |
|---|
| 137 | System.out.println(st.nextToken()); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | String parseFirstLine(String in, String ou) throws IOException { |
|---|
| 142 | BufferedReader fi = new BufferedReader(new FileReader(new File(in))); |
|---|
| 143 | BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou))); |
|---|
| 144 | String first_line, data; |
|---|
| 145 | first_line = fi.readLine(); |
|---|
| 146 | do { |
|---|
| 147 | data = fi.readLine(); |
|---|
| 148 | if (data == null) { |
|---|
| 149 | break; |
|---|
| 150 | } else { |
|---|
| 151 | fw.write(data + "\n"); |
|---|
| 152 | } |
|---|
| 153 | } while (true); |
|---|
| 154 | fw.close(); |
|---|
| 155 | return first_line; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | boolean deleteFile(String str) throws IOException { |
|---|
| 159 | File ttt = new File(str); |
|---|
| 160 | if (ttt.exists()) { |
|---|
| 161 | if (!ttt.delete()) { |
|---|
| 162 | System.err.print("delete file error !"); |
|---|
| 163 | } |
|---|
| 164 | } else { |
|---|
| 165 | System.out.println("file not exit!"); |
|---|
| 166 | } |
|---|
| 167 | return true; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void Inherbit() { |
|---|
| 171 | animal a = new animal(); |
|---|
| 172 | bird b = new bird(); |
|---|
| 173 | dog d = new dog(b); |
|---|
| 174 | animal aaa = new bird(); |
|---|
| 175 | // dog ddd = new animal(); -> error |
|---|
| 176 | |
|---|
| 177 | animal aa = (animal) b; |
|---|
| 178 | System.out.println(aa.hand); |
|---|
| 179 | System.out.println(aa.mind); |
|---|
| 180 | // bird bb = (bird) a; -> error |
|---|
| 181 | // d.setbird(aa); -> error |
|---|
| 182 | // System.out.println(aa.feather); -> error |
|---|
| 183 | |
|---|
| 184 | // Fu <T> |
|---|
| 185 | Fu<? extends String> ak; |
|---|
| 186 | ak = new Fu<String>(); |
|---|
| 187 | ak.getFu(); |
|---|
| 188 | Fu<?> in = ak; |
|---|
| 189 | // in.setFu("ss"); -> error |
|---|
| 190 | in.getFu(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void ttt() throws IOException { |
|---|
| 194 | Path pi; |
|---|
| 195 | JobConf conf = new JobConf(test.class); |
|---|
| 196 | FileStatus[] fi = FileSystem.get(conf).listStatus( |
|---|
| 197 | new Path("/user/waue/input/")); |
|---|
| 198 | for (int i = 0; i < fi.length; i++) { |
|---|
| 199 | pi = fi[i].getPath(); |
|---|
| 200 | System.out.println(pi.getName()); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void parseN() throws IOException { |
|---|
| 205 | BufferedReader br = new BufferedReader(new FileReader(new File( |
|---|
| 206 | "/home/waue/Desktop/a"))); |
|---|
| 207 | // BufferedWriter bw = new BufferedWriter(new FileWriter(new |
|---|
| 208 | // File("/home/waue/Desktop/b"))); |
|---|
| 209 | String line = br.readLine(); |
|---|
| 210 | // char n = '\n'; |
|---|
| 211 | int Count = 0; |
|---|
| 212 | do { |
|---|
| 213 | if (line.isEmpty()) { |
|---|
| 214 | System.out.println("empty !"); |
|---|
| 215 | } else { |
|---|
| 216 | System.out.println(line); |
|---|
| 217 | } |
|---|
| 218 | line = br.readLine(); |
|---|
| 219 | Count++; |
|---|
| 220 | } while (line != null); |
|---|
| 221 | |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void regular() { |
|---|
| 225 | Pattern patten_line; |
|---|
| 226 | Matcher matcher; |
|---|
| 227 | String logData = new String(); |
|---|
| 228 | String line = "[**] [1:2003:8] MS-SQL Worm propagation attempt [**]"; |
|---|
| 229 | // String line = "[**] [1:2189:3] BAD-TRAFFIC IP Proto 103 PIM [**]"; |
|---|
| 230 | patten_line = Pattern |
|---|
| 231 | .compile("^\\[\\**\\] \\[([0-9]*):([0-9]*):([0-9]*)\\] ([^\\[]*)\\[\\**\\]$"); |
|---|
| 232 | matcher = patten_line.matcher(line); |
|---|
| 233 | System.out.println("go"); |
|---|
| 234 | if (matcher.matches()) { |
|---|
| 235 | int number = matcher.groupCount(); |
|---|
| 236 | String[] data = new String[number]; |
|---|
| 237 | for (int j = 0; j < number; j++) { |
|---|
| 238 | data[j] = matcher.group(j + 1); |
|---|
| 239 | logData += (data[j] + ";"); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | System.out.println(logData); |
|---|
| 243 | } |
|---|
| 244 | System.out.println("end"); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | void enumTest() { |
|---|
| 248 | for (EnumTest t : EnumTest.values()) { |
|---|
| 249 | |
|---|
| 250 | System.out.println(t); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | void Date() throws Exception { |
|---|
| 256 | String da = "10/Jul/2008:08:26:55"; |
|---|
| 257 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", |
|---|
| 258 | Locale.US); |
|---|
| 259 | Date timestamp = sdf.parse(da); |
|---|
| 260 | long timeint = timestamp.getTime(); |
|---|
| 261 | System.out.println(timestamp.toString()); |
|---|
| 262 | System.out.println(timeint); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | void getNowTime() { |
|---|
| 266 | SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); |
|---|
| 267 | Date date = new Date(); |
|---|
| 268 | String strDate = sdFormat.format(date); |
|---|
| 269 | System.out.println(strDate); |
|---|
| 270 | System.out.println(date.getTime()); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | void setTime() { |
|---|
| 274 | String str = "07/08:01:01:01"; |
|---|
| 275 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM:HH:mm:ss", |
|---|
| 276 | Locale.TAIWAN); |
|---|
| 277 | Date tim = sdf.parse(str, new ParsePosition(0)); |
|---|
| 278 | System.out.println(tim); |
|---|
| 279 | Long timestamp = tim.getTime(); |
|---|
| 280 | System.out.println(timestamp); |
|---|
| 281 | |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | void selRegular(String str) { |
|---|
| 285 | Pattern patten_line1, patten_line2; |
|---|
| 286 | Matcher matcher; |
|---|
| 287 | String logData = new String(); |
|---|
| 288 | |
|---|
| 289 | patten_line1 = Pattern |
|---|
| 290 | .compile("^\\[Classification: ([^\\]]*)\\] \\[Priority: ([1-9]*)\\].*$"); |
|---|
| 291 | patten_line2 = Pattern.compile("^\\[Priority: ([1-9]*)\\].*$"); |
|---|
| 292 | matcher = patten_line1.matcher(str); |
|---|
| 293 | |
|---|
| 294 | if (matcher.matches()) { |
|---|
| 295 | int number = matcher.groupCount(); |
|---|
| 296 | String[] data = new String[number]; |
|---|
| 297 | for (int j = 0; j < number; j++) { |
|---|
| 298 | data[j] = matcher.group(j + 1); |
|---|
| 299 | logData += (data[j] + ";"); |
|---|
| 300 | } |
|---|
| 301 | } else { |
|---|
| 302 | matcher = patten_line2.matcher(str); |
|---|
| 303 | if (matcher.matches()) { |
|---|
| 304 | logData = "Port Scan;" + matcher.group(1) + ";"; |
|---|
| 305 | } else { |
|---|
| 306 | logData = ";;"; |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | System.out.println(logData); |
|---|
| 311 | System.out.println("end"); |
|---|
| 312 | |
|---|
| 313 | } |
|---|
| 314 | void splitExc(){ |
|---|
| 315 | String arr = "140.110.1.1:255"; |
|---|
| 316 | int n = arr.indexOf(":"); |
|---|
| 317 | if (n == -1) { |
|---|
| 318 | System.out.println("ip :" + arr); |
|---|
| 319 | } else { |
|---|
| 320 | System.out.println("ok"); |
|---|
| 321 | String[] vec = arr.split(":"); |
|---|
| 322 | System.out.println("ip :" + vec[0]); |
|---|
| 323 | System.out.println("port :" + vec[1]); |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | void callByRefFun(int a,int b){ |
|---|
| 327 | b = a; |
|---|
| 328 | } |
|---|
| 329 | void callByRef(){ |
|---|
| 330 | test t = new test(); |
|---|
| 331 | this.na =5; |
|---|
| 332 | int b = 10; |
|---|
| 333 | t.callByRefFun(b, this.na); |
|---|
| 334 | System.out.println(this.na); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | public static void main(String[] args) throws Exception { |
|---|
| 338 | |
|---|
| 339 | test t = new test(); |
|---|
| 340 | |
|---|
| 341 | // 測試 strToken函數 |
|---|
| 342 | // t.strToken(); |
|---|
| 343 | // 測試 檔案io 與 解析 |
|---|
| 344 | // System.out.println(a.parseFirstLine("/home/waue/test.txt", |
|---|
| 345 | // "/home/waue/out.tmp.txt")); |
|---|
| 346 | // 測試 listStatus |
|---|
| 347 | // t.ttt(); |
|---|
| 348 | // 測試 繼承 與 汎型 |
|---|
| 349 | // t.Inherbit() |
|---|
| 350 | // 測試 "換行" 是否為 "\n" |
|---|
| 351 | // t.parseN(); |
|---|
| 352 | // 測試 正規表示法 |
|---|
| 353 | // t.regular(); |
|---|
| 354 | // 測試 enum 列舉 |
|---|
| 355 | // t.enumTest(); |
|---|
| 356 | // 測試設定 |
|---|
| 357 | // t.Date(); |
|---|
| 358 | // 測試取得現在時間 |
|---|
| 359 | // t.getNowTime(); |
|---|
| 360 | // t.setTime(); |
|---|
| 361 | // 測試 選擇性正規表示法 |
|---|
| 362 | // t.selRegular(String str); |
|---|
| 363 | // 測試 split()的例外處理 |
|---|
| 364 | // t.splitExc(); |
|---|
| 365 | t.callByRef(); |
|---|
| 366 | |
|---|
| 367 | // if(vec[1] != null){ |
|---|
| 368 | // System.out.print(" \nport:" + vec[1] ); |
|---|
| 369 | // } |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | enum EnumTest { |
|---|
| 375 | R, L, GO |
|---|
| 376 | } |
|---|