1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
20
21
22 import org.apache.hadoop.chukwa.ChunkImpl;
23 import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
24 import org.apache.hadoop.chukwa.util.RecordConstants;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28
29
30
31
32
33
34 public class CharFileTailingAdaptorUTF8NewLineEscaped extends
35 FileTailingAdaptor {
36
37 private static final char SEPARATOR = '\n';
38
39 private ArrayList<Integer> offsets = new ArrayList<Integer>();
40
41
42
43
44
45
46
47
48 @Override
49 protected int extractRecords(ChunkReceiver eq, long buffOffsetInFile,
50 byte[] buf) throws InterruptedException {
51 String es = RecordConstants.RECORD_SEPARATOR_ESCAPE_SEQ;
52 for (int i = 0; i < buf.length; ++i) {
53
54 if (buf[i] == SEPARATOR) {
55
56 boolean escaped = false;
57 if (i - es.length() >= 0) {
58 escaped = true;
59
60 for (int j = 0; j < es.length(); j++) {
61 if (buf[i - es.length() + j] != es.charAt(j)) {
62 escaped = false;
63 }
64 }
65 }
66 if (!escaped) {
67 offsets.add(i);
68 }
69 }
70 }
71
72 if (offsets.size() > 0) {
73 int[] offsets_i = new int[offsets.size()];
74 for (int i = 0; i < offsets.size(); i++) {
75 try {
76 offsets_i[i] = offsets.get(i);
77 } catch(NullPointerException e) {
78
79 }
80 }
81
82 int bytesUsed = 0;
83 if(buf.length==offsets_i[offsets_i.length -1]) {
84
85
86 bytesUsed = offsets_i[offsets_i.length - 2] + 1;
87 } else {
88
89
90 bytesUsed = offsets_i[offsets_i.length - 1] + 1;
91 }
92
93 assert bytesUsed > 0 : " shouldn't send empty events";
94 ChunkImpl chunk = new ChunkImpl(type, toWatch.getAbsolutePath(),
95 buffOffsetInFile + bytesUsed, Arrays.copyOf(buf, bytesUsed), this);
96
97 chunk.setSeqID(buffOffsetInFile + bytesUsed);
98 chunk.setRecordOffsets(offsets_i);
99 eq.add(chunk);
100
101 offsets.clear();
102 return bytesUsed;
103 } else
104 return 0;
105 }
106
107 public String toString() {
108 return "escaped newline CFTA-UTF8";
109 }
110
111 }