1 46 51 package org.mr.core.util.byteable; 52 53 import java.io.IOException ; 54 55 60 public class ByteableByteArray implements Byteable { 61 62 byte[] payload; 63 64 67 public String getByteableName() { 68 return "BByteArray"; 69 } 70 71 74 public void toBytes(ByteableOutputStream out) throws IOException { 75 if(payload != null){ 76 out.writeInt(payload.length); 77 out.write(payload); 78 }else{ 79 out.writeInt(-1); 80 } 81 82 83 } 84 85 88 public Byteable createInstance(ByteableInputStream in) throws IOException { 89 ByteableByteArray result = new ByteableByteArray(); 90 int length = in.readInt(); 91 if(length != -1){ 92 byte[] load = new byte[length]; 93 in.readFully(load); 94 result.setPayload(load); 95 } 96 return result; 97 } 98 99 102 public void registerToByteableRegistry() { 103 ByteableRegistry.registerByteableFactory(getByteableName() , this); 104 105 } 106 107 public static void register(){ 108 ByteableByteArray instance = new ByteableByteArray(); 109 instance.registerToByteableRegistry(); 110 } 111 112 115 public byte[] getPayload() { 116 return payload; 117 } 118 119 122 public void setPayload(byte[] payload) { 123 this.payload = payload; 124 } 125 126 132 public void setPayload(byte[] orig , int startPos, int length){ 133 payload = new byte[length]; 134 System.arraycopy(orig,startPos,payload,0,length); 135 } 136 137 } 138 | Popular Tags |