01 package net.sf.jbaobab.io.impl;
02
03 import java.nio.ByteBuffer;
04
05 import net.sf.jbaobab.io.AbstractSeperator;
06
07 /**
08 * <p>
09 * A {@link net.sf.jbaobab.io.Seperator} that takes CRLF/LF as delimeter.
10 * </p>
11 *
12 * @author Oakyoon Cha
13 */
14 public class CRLFSeperator
15 extends AbstractSeperator {
16
17 public boolean ready(ByteBuffer source) {
18 int n = source.position();
19 for (int i = 0; i < n; i++) {
20 if (source.get(i) == '\n') {
21 cursor(i + 1);
22 return true;
23 }
24 }
25 return false;
26 }
27
28 public ByteBuffer seperate(ByteBuffer source) {
29 ByteBuffer seperated = super.seperate(source);
30 int position = seperated.limit() - 1;
31 seperated.position(position);
32 if (position > 0 && seperated.get(position - 1) == '\r')
33 seperated.position(position - 1);
34 seperated.flip();
35 return seperated;
36 }
37
38 public ByteBuffer terminate(ByteBuffer source) {
39 ByteBuffer terminated = ByteBuffer.allocate(source.limit() + 2);
40 terminated.put(source);
41 terminated.put((byte) '\r');
42 terminated.put((byte) '\n');
43 terminated.flip();
44 return terminated;
45 }
46
47 }
|