Permalink
Browse files

add StringSerializer supported

add some comments
  • Loading branch information...
adyliu committed May 9, 2012
1 parent 6033836 commit 56bf03d6173deb82f22bc2e50ada0ff8f9ad00da
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.github.zkclient.serialize;
+
+import java.io.UnsupportedEncodingException;
+
+import com.github.zkclient.exception.ZkMarshallingError;
+
+/**
+ * String Encoder(UTF-8 default)
+ *
+ * @author adyliu (imxylz@gmail.com)
+ * @since 2.0
+ */
+public class StringSerializer implements ZkSerializer {
+
+ private final String encode;
+
+ public StringSerializer() {
+ this("UTF-8");
+ }
+
+ public StringSerializer(String encode) {
+ this.encode = encode;
+ }
+
+ @Override
+ public byte[] serialize(Object data) throws ZkMarshallingError {
+ try {
+ return data == null ? null : ((String) data).getBytes(encode);
+ } catch (UnsupportedEncodingException e) {
+ throw new ZkMarshallingError(e);
+ }
+ }
+
+ @Override
+ public Object deserialize(byte[] bytes) throws ZkMarshallingError {
+ try {
+ return bytes == null ? (String) null : new String(bytes, encode);
+ } catch (UnsupportedEncodingException e) {
+ throw new ZkMarshallingError(e);
+ }
+ }
+
+}
@@ -18,15 +18,30 @@
import com.github.zkclient.exception.ZkMarshallingError;
/**
- * Zookeeper is able to store data in form of byte arrays. This interfacte is a bridge between those byte-array format
- * and higher level objects.
+ * Zookeeper is able to store data in form of byte arrays. This interfacte is a bridge between
+ * those byte-array format and higher level objects.
*
* @see BytesPushThroughSerializer
* @see SerializableSerializer
+ * @see StringSerializer
*/
public interface ZkSerializer {
- public byte[] serialize(Object data) throws ZkMarshallingError;
+ /**
+ * serialize object to byte arrays
+ *
+ * @param data object
+ * @return byte arrays
+ * @throws ZkMarshallingError
+ */
+ byte[] serialize(Object data) throws ZkMarshallingError;
- public Object deserialize(byte[] bytes) throws ZkMarshallingError;
+ /**
+ * convert byte arrays to object
+ *
+ * @param bytes byte arrays
+ * @return object
+ * @throws ZkMarshallingError
+ */
+ Object deserialize(byte[] bytes) throws ZkMarshallingError;
}

0 comments on commit 56bf03d

Please sign in to comment.