1 46 50 package org.mr.core.protocol; 51 52 import java.io.IOException ; 53 import java.nio.ByteBuffer ; 54 55 import org.mr.core.util.byteable.ByteBufferFactory; 56 import org.mr.core.util.byteable.ByteBufferPool; 57 import org.mr.core.util.byteable.ByteBufferPoolParameters; 58 import org.mr.core.util.byteable.Byteable; 59 import org.mr.core.util.byteable.ByteableInputStream; 60 import org.mr.core.util.byteable.ByteableInputStreamPool; 61 import org.mr.core.util.byteable.ByteableOutputStream; 62 import org.mr.core.util.byteable.UnboundedByteBufferPool; 63 64 69 public class MessageTransformer { 70 71 public static UnboundedByteBufferPool messageHeaderBufferPool = null; 72 73 public static UnboundedByteBufferPool messageBodyBufferPool = null; 74 75 public MessageTransformer(){ 76 ByteBufferPoolParameters params = new ByteBufferPoolParameters(); 78 params.setSmallBufferSize(400); 80 params.setMediumBufferSize(1024); 81 params.setLargeBufferSize(2048); 82 83 params.setNumInSmallPool(200); 84 params.setNumInMediumPool(50); 85 params.setNumInLargePool(20); 86 87 messageHeaderBufferPool = new UnboundedByteBufferPool(params); 88 89 params = new ByteBufferPoolParameters(); 91 params.setSmallBufferSize(1500); 93 params.setMediumBufferSize(5400); 94 params.setLargeBufferSize(11000); 95 96 params.setNumInSmallPool(1000); 97 params.setNumInMediumPool(150); 98 params.setNumInLargePool(10); 99 100 messageBodyBufferPool = new UnboundedByteBufferPool(params); 101 102 } 103 104 110 public static final ByteBuffer byteableToBuffer(Byteable byteable ,ByteBufferFactory pool) throws IOException { 111 ByteableOutputStream out = new ByteableOutputStream( pool); 112 out.writeByteable(byteable); 113 return out.getByteBuffer(); 114 } 116 122 public static final ByteBuffer toBuffer(MantaBusMessage msg,ByteBufferPool pool ) throws IOException { 123 ByteableOutputStream out = new ByteableOutputStream( pool); 124 out.writeByteable(msg); 125 return out.getByteBuffer(); 126 } 128 129 130 135 public final static MantaBusMessage fromBuffer(ByteBuffer buffer) throws IOException { 136 return (MantaBusMessage)byteableFormBuffer( buffer); 137 } 138 143 public final static Byteable byteableFormBuffer(ByteBuffer buffer)throws IOException { 144 ByteableInputStream in = null; 145 Byteable result = null; 146 try{ 147 in = (ByteableInputStream)ByteableInputStreamPool.getInstance().acquireObject(); 148 in.setUnderLine(buffer); 149 result =in.readByteable(); 150 }finally{ 151 if(in!= null){ 152 in.release(); 153 } 154 } 155 156 return result; 157 } 158 159 160 } 161 162 163 164 | Popular Tags |