aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/construct/parsers/ByteFillParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/gnunet/construct/parsers/ByteFillParser.java')
-rw-r--r--src/org/gnunet/construct/parsers/ByteFillParser.java100
1 files changed, 0 insertions, 100 deletions
diff --git a/src/org/gnunet/construct/parsers/ByteFillParser.java b/src/org/gnunet/construct/parsers/ByteFillParser.java
deleted file mode 100644
index a581229..0000000
--- a/src/org/gnunet/construct/parsers/ByteFillParser.java
+++ /dev/null
@@ -1,100 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.construct.parsers;
22
23import org.gnunet.construct.Message;
24import org.gnunet.construct.ReflectUtil;
25import org.gnunet.construct.ProtocolViolation;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.lang.reflect.Array;
30import java.lang.reflect.Field;
31import java.nio.ByteBuffer;
32import java.util.List;
33
34/**
35 * Parse a byte array that takes up all the remaining space in its frame.
36 * A 0-element array takes up no space.
37 * 'null' is an invalid value for a field of this type.
38 *
39 * @author Florian Dold
40 *
41 */
42public class ByteFillParser implements Parser {
43 private static final Logger logger = LoggerFactory
44 .getLogger(ByteFillParser.class);
45
46 private final Field targetField;
47
48
49 public ByteFillParser(Field field) {
50 targetField = field;
51 }
52
53 @Override
54 public int getSize(Message src) {
55 return Array.getLength(ReflectUtil.justGet(src, targetField));
56 }
57
58
59 @Override
60 public int parse(ByteBuffer srcBuf, int frameOffset, Message frameObj, Message dst, List<Field> frameSizePath) {
61 int frameSize = ReflectUtil.justGetInt(frameObj, frameSizePath);
62 int remaining = frameOffset + frameSize - srcBuf.position();
63
64 if (remaining < 0) {
65 throw new ProtocolViolation("negative size remaining for variable size message");
66 }
67
68 if (remaining == 0) {
69 ReflectUtil.justSet(dst, targetField, new byte[0]);
70 return 0;
71 }
72
73 byte[] a = new byte[remaining];
74
75 srcBuf.get(a);
76
77 ReflectUtil.justSet(dst, targetField, a);
78
79 return frameOffset;
80 }
81
82 @Override
83 public int write(ByteBuffer dstBuf, Message src) {
84 byte[] a = (byte[]) ReflectUtil.justGet(src, targetField);
85 dstBuf.put(a);
86 return a.length;
87 }
88
89 @Override
90 public void patch(Message message, int frameSize, List<Field> frameSizePath, Message frameObject) {
91 // the size field is contained in the frameObject
92 ReflectUtil.justSetInt(frameObject, frameSizePath, frameSize);
93 }
94
95 @Override
96 public int getStaticSize() {
97 // not known!
98 return 0;
99 }
100}