summaryrefslogtreecommitdiff
path: root/gnu/java/nio/GenericBuffer.cpp
blob: 07e5400521eeaddf2ebd4380918c58ee4edad4d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package manta.runtime;

import java.nio.*;

// these are wrappers around byte[]'s


#include "temp.h"

public final class BUFFERImpl extends java.nio.  BUFFER
{
    private int array_offset;
    ELT [] backing_buffer;
    private boolean ro;

    BUFFERImpl(int cap, int off, int lim)
    {
      this.backing_buffer = new ELT[cap];
      this.cap = cap;
      this.pos = off;
      this.limit = lim;
    }

    BUFFERImpl(ELT[] array, int off, int lim)
    {
      this.backing_buffer = array;
      this.cap = array.length;
      this.pos = off;
      this.limit = lim;
    }

    BUFFERImpl(BUFFERImpl copy)
    {
	backing_buffer = copy.backing_buffer;
	ro             = copy.ro;
	pos            = copy.pos;
	limit          = copy.limit;
    }

    void inc_pos(int a)
    {
	pos += a;
    }

  private static MantaNative ELT[] nio_cast(byte[]copy);
  private static MantaNative ELT[] nio_cast(char[]copy);
  private static MantaNative ELT[] nio_cast(short[]copy);
  private static MantaNative ELT[] nio_cast(long[]copy);
  private static MantaNative ELT[] nio_cast(int[]copy);
  private static MantaNative ELT[] nio_cast(float[]copy);
  private static MantaNative ELT[] nio_cast(double[]copy);

#define CAST_CTOR(ELT,TO_TYPE) \
    BUFFERImpl(ELT[] copy) \
    { \
	this.backing_buffer = copy != null ? nio_cast(copy) : null; \
    }  \
\
\
\
    private static MantaNative ELT nio_get_ ## TO_TYPE(BUFFERImpl b, int index); \
\
\
    private static MantaNative void nio_put_ ## TO_TYPE(BUFFERImpl b, int index, ELT value);\
\
\
   public java.nio. TO_TYPE ## Buffer as ## TO_TYPE ## Buffer() \
  { \
    return new manta.runtime. TO_TYPE ## BufferImpl(backing_buffer); \
  } 


  CAST_CTOR(byte,Byte)
  CAST_CTOR(char,Char)
  CAST_CTOR(short,Short)
  CAST_CTOR(int,Int)
  CAST_CTOR(long,Long)
  CAST_CTOR(float,Float)
  CAST_CTOR(double,Double)


    public  boolean isReadOnly() 
    {
	return ro; 
    }

    public  java.nio. BUFFER slice() 
    {
	BUFFERImpl A = new BUFFERImpl(this);
	A.array_offset = pos;
	return A;
    }

    public  java.nio. BUFFER duplicate()
    {	
	return new BUFFERImpl(this);
    }

    public  java.nio. BUFFER asReadOnlyBuffer() 
    {	
	BUFFERImpl a = new BUFFERImpl(this);
	a.ro = true;
	return a; 
    }

    public  java.nio. BUFFER compact() 
    {
	return this; 
    }

    public  boolean isDirect()
    {
	return backing_buffer != null; 
    }

    public ELT get()
    {
	ELT e = backing_buffer[pos];
	pos++;
	return e;
    }
    
    public java.nio. BUFFER put(ELT  b)
    {
	backing_buffer[pos] = b;
	pos++;
	return this;
    }
    public ELT get(int index)
    {
	return backing_buffer[index];
    }
    public java.nio. BUFFER put(int index, ELT  b)
    {
      backing_buffer[index] = b;
      return this;
    }
    

#define NATIVE_GET_PUT(TYPE,SIZE,ELT)					\
    public  ELT get ## TYPE()                        			\
    {									\
	ELT a = nio_get_ ## TYPE(this, pos); 				\
	inc_pos(SIZE);							\
	return a;							\
    }									\
    public  java.nio. BUFFER put ## TYPE(ELT  value) 			\
    {									\
        nio_put_ ## TYPE(this, pos, value);				\
	inc_pos(SIZE);							\
	return this; 							\
    }									\
    public  ELT get ## TYPE(int  index)					\
    {									\
	ELT a = nio_get_ ## TYPE(this, index); 				\
	inc_pos(SIZE);							\
	return a;							\
    }									\
    public  java.nio. BUFFER put ## TYPE(int  index, ELT  value)	\
    {									\
	nio_put_ ## TYPE(this, index, value); 			\
	inc_pos(SIZE);							\
	return this;							\
    }

#define INLINE_GET_PUT(TYPE,ELT)				\
    public  ELT get ## TYPE() 					\
    {								\
	return get();						\
    }								\
    public  java.nio. BUFFER put ## TYPE(ELT  value)		\
    {								\
	return put(value);					\
    }								\
    public  ELT get ## TYPE(int  index)				\
    {								\
	return get(index);					\
    }								\
    public  java.nio. BUFFER put ## TYPE(int  index, ELT  value)	\
    {								\
	return put(index, value);				\
    }


#if defined(CHAR)
  INLINE_GET_PUT(Char, char);
#else 
  NATIVE_GET_PUT(Char,2,char);
#endif


#if defined(SHORT)
  INLINE_GET_PUT(Short, short);
#else
  NATIVE_GET_PUT(Short,2,short);
#endif

#if defined(INT)
  INLINE_GET_PUT(Int,int);
#else
  NATIVE_GET_PUT(Int,4,int);
#endif

#if defined(LONG)
  INLINE_GET_PUT(Long,long);
#else
  NATIVE_GET_PUT(Long,8,long);
#endif

#if defined(FLOAT)
  INLINE_GET_PUT(Float,float);
#else
  NATIVE_GET_PUT(Float,4,float);
#endif

#if defined(DOUBLE)
  INLINE_GET_PUT(Double,double);
#else  
  NATIVE_GET_PUT(Double,8,double);
#endif


#if defined(CHAR)
    public String toString()
    { 
      if (backing_buffer != null)
	{
	  return new String(backing_buffer, position(), limit());
	}
      return super.toString();
    }  
#endif
}