PageRenderTime 50ms CodeModel.GetById 20ms app.highlight 25ms RepoModel.GetById 2ms app.codeStats 0ms

/ictclas4j/src/org/ictclas4j/bean/WordFingerprint.java

http://ictclas4j.googlecode.com/
Java | 142 lines | 102 code | 25 blank | 15 comment | 17 complexity | dd128d4ccf60b4e9a42736695bb6eb8b MD5 | raw file
  1package org.ictclas4j.bean;
  2
  3import java.io.DataInputStream;
  4import java.io.DataOutputStream;
  5import java.io.IOException;
  6
  7/**
  8 * ??????
  9 * 
 10 * @author sinboy
 11 * @since 2008.7.5
 12 * 
 13 */
 14public class WordFingerprint {
 15	private String word;
 16
 17	// ????
 18	private String[] pinyin;
 19
 20	// ??????????????????->4425134
 21	private String fingerprint;
 22
 23	public String getWord() {
 24		return word;
 25	}
 26
 27	public void setWord(String word) {
 28		this.word = word;
 29	}
 30
 31	public String[] getPinyin() {
 32		return pinyin;
 33	}
 34
 35	public void setPinyin(String[] pinyin) {
 36		this.pinyin = pinyin;
 37	}
 38
 39	public String getFingerprint() {
 40		return fingerprint;
 41	}
 42
 43	public void setFingerprint(String fingerprint) {
 44		this.fingerprint = fingerprint;
 45	}
 46
 47	public String toString() {
 48		StringBuffer sb = new StringBuffer();
 49		sb.append(word).append(",");
 50		sb.append(fingerprint).append(",");
 51		for (int i = 0; i < pinyin.length; i++) {
 52			sb.append(pinyin[i]);
 53			if (i != pinyin.length - 1)
 54				sb.append("/");
 55		}
 56		return sb.toString();
 57	}
 58
 59	public int read(DataInputStream in, long offset) throws IOException {
 60		int size = 0;
 61
 62		if (in != null) {
 63
 64			in.skip(offset);
 65
 66			// read word
 67			int wordLen = in.readByte();
 68			byte[] bword = new byte[wordLen];
 69			in.read(bword);
 70			size += 1 + wordLen;
 71			word = new String(bword);
 72
 73			// read fingerprint
 74			int fpLen = in.readByte();
 75			byte[] bfp = new byte[fpLen];
 76			in.read(bfp);
 77			size += 1 + fpLen;
 78			fingerprint = new String(bfp);
 79
 80			// read pinyin
 81			int pinyinCount = in.readByte();
 82			size++;
 83			pinyin = new String[pinyinCount];
 84			for (int i = 0; i < pinyinCount; i++) {
 85				int pinyinLen = in.readByte();
 86				byte[] bpinyin = new byte[pinyinLen];
 87				in.read(bpinyin);
 88				pinyin[i] = new String(bpinyin);
 89				size += 1 + pinyinLen;
 90			}
 91
 92		}
 93		return size;
 94	}
 95
 96	public int write(DataOutputStream out) throws IOException {
 97		int size = 0;
 98
 99		if (out != null) {
100			// write word
101			int wordLen = word != null ? word.getBytes().length : 0;
102			out.writeByte((byte) wordLen);
103			size++;
104			if (wordLen > 0) {
105				byte[] b = word.getBytes();
106				out.write(b);
107				size += wordLen;
108			}
109
110			// write fingerprint
111			int fpLen = fingerprint != null ? fingerprint.getBytes().length : 0;
112			out.writeByte((byte) fpLen);
113			size++;
114			if (fpLen > 0) {
115				byte[] b = fingerprint.getBytes();
116				out.write(b);
117				size += fpLen;
118			}
119
120			// write pinyin
121			int pinyinCount = pinyin != null ? pinyin.length : 0;
122			out.writeByte((byte) pinyinCount);
123			size++;
124			if (pinyinCount > 0) {
125				for (int i = 0; i < pinyinCount; i++) {
126					int pinyinLen = pinyin[i] != null ? pinyin[i].getBytes().length : 0;
127					out.writeByte(pinyinLen);
128					size++;
129					if (pinyinLen > 0) {
130						byte[] b = pinyin[i].getBytes();
131						out.write(b);
132						size += pinyinLen;
133					}
134				}
135			}
136
137		}
138		return size;
139
140	}
141
142}