Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fd3e58cab39cbaa74f43efc75eeb3dc6a67b06f (plain) (blame)
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
/**
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.internal.net4j.buffer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
 * @author Eike Stepper
 */
public final class BufferUtil
{
  private static final byte FALSE = (byte)0;

  private static final byte TRUE = (byte)1;

  public static final String UTF8_CHAR_SET_NAME = "UTF-8"; //$NON-NLS-1$

  private BufferUtil()
  {
  }

  public static byte[] toUTF8(String str)
  {
    if (str == null)
    {
      return new byte[0];
    }

    try
    {
      byte[] bytes = str.getBytes(UTF8_CHAR_SET_NAME);
      String test = new String(bytes, UTF8_CHAR_SET_NAME);
      if (!str.equals(test))
      {
        throw new IllegalArgumentException("String not encodable: " + str); //$NON-NLS-1$
      }

      return bytes;
    }
    catch (UnsupportedEncodingException ex)
    {
      // This should really not happen
      throw new RuntimeException(ex);
    }
  }

  public static String fromUTF8(byte[] bytes)
  {
    try
    {
      return new String(bytes, UTF8_CHAR_SET_NAME);
    }
    catch (UnsupportedEncodingException ex)
    {
      // This should really not happen
      throw new RuntimeException(ex);
    }
  }

  public static void putObject(ByteBuffer byteBuffer, Object object) throws IOException
  {
    if (object != null)
    {
      byteBuffer.put(TRUE);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream stream = new ObjectOutputStream(baos);
      stream.writeObject(object);

      byte[] array = baos.toByteArray();
      putByteArray(byteBuffer, array);
    }
    else
    {
      byteBuffer.put(FALSE);
    }
  }

  public static Object getObject(ByteBuffer byteBuffer) throws IOException, ClassNotFoundException
  {
    boolean nonNull = byteBuffer.get() == TRUE;
    if (nonNull)
    {
      byte[] array = getByteArray(byteBuffer);
      ByteArrayInputStream bais = new ByteArrayInputStream(array);
      ObjectInputStream stream = new ObjectInputStream(bais);
      return stream.readObject();
    }

    return null;
  }

  public static void putByteArray(ByteBuffer byteBuffer, byte[] array)
  {
    byteBuffer.putShort((short)array.length);
    if (array.length != 0)
    {
      byteBuffer.put(array);
    }
  }

  public static byte[] getByteArray(ByteBuffer byteBuffer)
  {
    short length = byteBuffer.getShort();
    byte[] array = new byte[length];
    if (length != 0)
    {
      byteBuffer.get(array);
    }

    return array;
  }

  public static void putUTF8(ByteBuffer byteBuffer, String str)
  {
    byte[] bytes = BufferUtil.toUTF8(str);
    if (bytes.length > byteBuffer.remaining())
    {
      throw new IllegalArgumentException("String too long: " + str); //$NON-NLS-1$
    }

    putByteArray(byteBuffer, bytes);
  }
}

Back to the top