コード例 #1
0
ファイル: DataSerializer.java プロジェクト: BenKoerber/TomP2P
 @Override
 public void serialize(DataOutput out, Data value) throws IOException {
   if (value.length() > MAX_SIZE) {
     // header, 1 means stored on disk in a file
     out.writeByte(1);
     serializeFile(out, value);
   } else {
     // header, 0 means stored on disk with MapDB
     out.writeByte(0);
     serializeMapDB(out, value);
   }
 }
コード例 #2
0
 public static Data createRandomData(int size, int number) {
   int i = r.nextInt();
   i = (i % 127);
   int length = Math.abs(r.nextInt() % size);
   if (length < 100) length += 100;
   Data d = new Data();
   d.length = length;
   d.key = (byte) i;
   d.data = new byte[length];
   Arrays.fill(d.data, d.key);
   if (number > 0 && d.data.length >= 4) {
     // populate number
     d.hasNr = true;
     XByteBuffer.toBytes(number, d.data, 0);
   }
   return d;
 }
コード例 #3
0
ファイル: DataSerializer.java プロジェクト: BenKoerber/TomP2P
 private Data deserializeMapDB(DataInput in) throws IOException {
   ByteBuf buf = Unpooled.buffer();
   Data data = null;
   while (data == null) {
     buf.writeByte(in.readByte());
     data = Data.decodeHeader(buf, signatureFactory);
   }
   int len = data.length();
   byte me[] = new byte[len];
   in.readFully(me);
   buf = Unpooled.wrappedBuffer(me);
   boolean retVal = data.decodeBuffer(buf);
   if (!retVal) {
     throw new IOException("data could not be read");
   }
   retVal = data.decodeDone(buf, signatureFactory);
   if (!retVal) {
     throw new IOException("signature could not be read");
   }
   return data;
 }