File: Buffer.java

package info (click to toggle)
libpixie-java 1%3A1.1.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,076 kB
  • sloc: java: 10,175; xml: 1,579; makefile: 13
file content (310 lines) | stat: -rw-r--r-- 7,783 bytes parent folder | download | duplicates (3)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2000 - 2009 Pentaho Corporation, Object Refinery Limited and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.pixie.wmf;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

/**
 * A block of raw mmeory. This is used to store various metafile objects as they are read
 * in from file.
 */
public class Buffer
{
  /**
   * The memory itself.
   */
  private byte[] bytes;

  /**
   * The current length of the memory.
   */
  private int length;

  /**
   * Default Constructor. Defines a buffer without an initial size.
   */
  protected Buffer()
  {
  }

  /**
   * Defines a new buffer with the given initial size in bytes.
   *
   * @param length the length of the buffer in bytes.
   */
  protected Buffer(final int length)
  {
    setCapacity(length);
  }

  /**
   * The size of the stored data in the memory.
   */
  public final int getLength()
  {
    return length;
  }

  /**
   * Extends the length to the given new size.
   *
   * @param len the new length.
   * @throws IllegalArgumentException if the length is shorter than the used storage in
   *                                  memory.
   */
  protected void setLength(final int len)
  {
    if (len > bytes.length)
    {
      throw new IllegalArgumentException();
    }

    this.length = len;
  }

  /**
   * Ensures that the buffer has enough space for the given number of bytes.
   *
   * @param capacity the new capacity that should be ensured.
   * @throws IllegalArgumentException if the capacity is smaller than the buffers length.
   */
  protected void setCapacity(final int capacity)
  {
    if (capacity < getLength())
    {
      throw new IllegalArgumentException();
    }

    if (bytes == null || bytes.length == 0)
    {
      bytes = new byte[capacity];
    }
    else if (capacity != bytes.length)
    {
      final byte[] old = bytes;
      bytes = new byte[capacity];
      System.arraycopy(old, 0, bytes, 0, Math.min(old.length, capacity));
    }
  }

  /**
   * Read <code>len</code> bytes into the memory from a stream and stores the read bytes
   * at the given offset.
   *
   * @param in     the input stream that should be used
   * @param offset the offset
   * @param len    the number bytes that should be read.
   */
  public void read(final InputStream in, int offset, int len)
      throws IOException
  {
    // make sure, that all bytes can be read and create the buffer if needed.
    if (bytes == null || offset + len > bytes.length)
    {
      setCapacity(offset + len);
    }

    //in.readFully( bytes, offset, len );
    while (len > 0)
    {
      final int blockSize = in.read(bytes, offset, len);
      if (blockSize <= 0)
      {
        throw new EOFException();
      }
      offset += blockSize;
      len -= blockSize;
      setLength(offset);
    }
  }

  /**
   * Moves the buffer contents from the source offset to the target offset, the areas
   * should not overlap.
   *
   * @param sourceoffset
   * @param length
   * @param targetoffset
   */
  protected void move(final int sourceoffset, final int length, final int targetoffset)
  {
    System.arraycopy(bytes, sourceoffset, bytes, targetoffset, length);
  }

  /**
   * Set the int value as big-endian.
   *
   * @param offset the offset where to set the int value.
   * @param value  the integer value that should be set.
   */
  public void setInt(final int offset, final int value)
  {
    if (offset > (getLength() - 4))
    {
      throw new IndexOutOfBoundsException();
    }

    setShort(offset, value & 0x0ffff);
    setShort(offset + 2, value >> 16);
  }

  /**
   * Return the 32-bit int at the given byte offset.
   *
   * @param offset the offset where the integer value is stored in the memory
   * @return the integer.
   */
  public int getInt(final int offset)
  {
    if (offset > (getLength() - 4))
    {
      throw new IndexOutOfBoundsException();
    }

    return (getShort(offset) & 0x0ffff) | (getShort(offset + 2) << 16);
  }

  /**
   * Stores the given short as BigEndian value.
   *
   * @param offset   the offset.
   * @param shortval the shortvalue.
   */
  public void setShort(final int offset, final int shortval)
  {
    if (offset > (getLength() - 2))
    {
      throw new IndexOutOfBoundsException();
    }

    bytes[offset] = (byte) (shortval & 0x0ff);
    bytes[offset + 1] = (byte) (shortval >> 8);
  }

  /**
   * Return the 16-bit int at the given byte offset.
   *
   * @param offset the offset from where to read the short.
   * @return the short.
   */
  public int getShort(final int offset)
  {
    if (offset > (getLength() - 2))
    {
      throw new IndexOutOfBoundsException
          ("Offset " + offset + " is out of limit. " +
              "Max length is " + (getLength() - 2));
    }

    return (bytes[offset] & 0x0ff) | (bytes[offset + 1] << 8);
  }

  /**
   * Sets the byte at the given offset.
   *
   * @param offset the offset.
   * @param value  the byte that should be set.
   */
  public void setByte(final int offset, final int value)
  {
    if (offset > (getLength() - 1))
    {
      throw new IndexOutOfBoundsException();
    }

    bytes[offset] = (byte) (value & 0x0ff);
  }

  /**
   * Return the 8-bit int at the given byte offset.
   *
   * @param offset the offset from where to read the byte
   * @return the byte read.
   */
  public int getByte(final int offset)
  {
    if (offset > (getLength() - 1))
    {
      throw new IndexOutOfBoundsException();
    }

    return bytes[offset] & 0x0ff;
  }

  /**
   * Writes the given string as byte stream using the plattforms default encoding.
   *
   * @param offset the offset, where to store the string.
   * @param str    the string that should be stored in the Wmf.
   */
  public void setString(final int offset, final String str)
  {
    if ((offset + str.length()) > (getLength() - 1))
    {
      throw new IndexOutOfBoundsException();
    }

    final byte[] b = str.getBytes();

    final int len = getLength() - offset;

    for (int i = 0; i < len; i++)
    {
      bytes[offset + i] = b[offset];
    }
    if ((offset + len) < getLength())
    {
      bytes[offset + len] = 0;
    }
  }

  /**
   * Return the null-terminated string at the given byte offset with the given maximum
   * length.
   *
   * @param offset the offset where the string starts
   * @param len    the maximum length of the string
   * @return the null-terminated string read.
   */
  public String getString(final int offset, final int len)
  {
    int i;
    for (i = 0; i < len; i++)
    {
      if (bytes[offset + i] == 0)
      {
        break;
      }
    }
    return new String(bytes, offset, i);
  }

  /**
   * Gets an input stream to read from the memory buffer.
   *
   * @param offset the offse, from where to read.
   * @return the InputStream.
   */
  public InputStream getInputStream(final int offset)
  {
    return new ByteArrayInputStream(bytes, offset, bytes.length - offset);
  }
}