1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
202122import org.apache.hadoop.chukwa.ChunkImpl;
23import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
24import org.apache.hadoop.chukwa.util.RecordConstants;
2526import java.util.ArrayList;
27import java.util.Arrays;
2829/**30 * A subclass of FileTailingAdaptor that reads UTF8/ascii files and splits31 * records at non-escaped carriage returns32 * 33 */34publicclassCharFileTailingAdaptorUTF8NewLineEscapedextends35FileTailingAdaptor {
3637privatestaticfinalchar SEPARATOR = '\n';
3839private ArrayList<Integer> offsets = new ArrayList<Integer>();
4041/**42 * 43 * Note: this method uses a temporary ArrayList (shared across instances).44 * This means we're copying ints each time. This could be a performance issue.45 * Also, 'offsets' never shrinks, and will be of size proportional to the46 * largest number of lines ever seen in an event.47 */48 @Override
49protectedint extractRecords(ChunkReceiver eq, long buffOffsetInFile,
50 byte[] buf) throws InterruptedException {
51 String es = RecordConstants.RECORD_SEPARATOR_ESCAPE_SEQ;
52for (int i = 0; i < buf.length; ++i) {
53// if this is a separator54if (buf[i] == SEPARATOR) {
55// if possibly preceded by escape sequence (avoid outOfBounds here)56boolean escaped = false; // was it escaped?57if (i - es.length() >= 0) {
58 escaped = true; // maybe (at least there was room for an escape59// sequence, so let's check for the e.s.)60for (int j = 0; j < es.length(); j++) {
61if (buf[i - es.length() + j] != es.charAt(j)) {
62 escaped = false;
63 }
64 }
65 }
66if (!escaped) {
67 offsets.add(i);
68 }
69 }
70 }
7172if (offsets.size() > 0) {
73int[] offsets_i = newint[offsets.size()];
74for (int i = 0; i < offsets.size(); i++) {
75try {
76 offsets_i[i] = offsets.get(i);
77 } catch(NullPointerException e) {
78// Skip offsets 0 where it can be null.79 }
80 }
81// make the stream unique to this adaptor82int bytesUsed = 0;
83if(buf.length==offsets_i[offsets_i.length -1]) {
84// If Separator is last character of stream,85// send the record.86 bytesUsed = offsets_i[offsets_i.length - 2] + 1;
87 } else {
88// If the last record is partial read,89// truncate the record to the n -1 new line.90 bytesUsed = offsets_i[offsets_i.length - 1] + 1; // char at last 91 }
92// offset uses a byte93 assert bytesUsed > 0 : " shouldn't send empty events";
94ChunkImpl chunk = newChunkImpl(type, toWatch.getAbsolutePath(),
95 buffOffsetInFile + bytesUsed, Arrays.copyOf(buf, bytesUsed), this);
9697 chunk.setSeqID(buffOffsetInFile + bytesUsed);
98 chunk.setRecordOffsets(offsets_i);
99 eq.add(chunk);
100101 offsets.clear();
102return bytesUsed;
103 } else104return 0;
105 }
106107public String toString() {
108return"escaped newline CFTA-UTF8";
109 }
110111 }