propane 3.6.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG.md +5 -1
  5. data/README.md +6 -13
  6. data/Rakefile +7 -6
  7. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  8. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  9. data/lib/java/monkstone/ColorUtil.java +127 -0
  10. data/lib/java/monkstone/MathToolModule.java +287 -0
  11. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  12. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  13. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  14. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  15. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  16. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  17. data/lib/java/monkstone/noise/FastTerrain.java +874 -0
  18. data/lib/java/monkstone/noise/Noise.java +90 -0
  19. data/lib/java/monkstone/noise/NoiseGenerator.java +75 -0
  20. data/lib/java/monkstone/noise/NoiseMode.java +28 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  22. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  23. data/lib/java/monkstone/noise/SmoothTerrain.java +1099 -0
  24. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  25. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  26. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  27. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  28. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  29. data/lib/java/monkstone/slider/Slider.java +61 -0
  30. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  31. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  32. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  33. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  34. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  35. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  36. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  37. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  38. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  39. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  40. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  41. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  42. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  43. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  44. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  45. data/lib/java/processing/awt/PImageAWT.java +377 -0
  46. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  47. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  48. data/lib/java/processing/awt/ShimAWT.java +581 -0
  49. data/lib/java/processing/core/PApplet.java +15156 -0
  50. data/lib/java/processing/core/PConstants.java +523 -0
  51. data/lib/java/processing/core/PFont.java +1126 -0
  52. data/lib/java/processing/core/PGraphics.java +8600 -0
  53. data/lib/java/processing/core/PImage.java +3377 -0
  54. data/lib/java/processing/core/PMatrix.java +208 -0
  55. data/lib/java/processing/core/PMatrix2D.java +562 -0
  56. data/lib/java/processing/core/PMatrix3D.java +890 -0
  57. data/lib/java/processing/core/PShape.java +3561 -0
  58. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  59. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  60. data/lib/java/processing/core/PStyle.java +63 -0
  61. data/lib/java/processing/core/PSurface.java +198 -0
  62. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  63. data/lib/java/processing/core/PVector.java +1066 -0
  64. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  65. data/lib/java/processing/data/DoubleDict.java +850 -0
  66. data/lib/java/processing/data/DoubleList.java +928 -0
  67. data/lib/java/processing/data/FloatDict.java +847 -0
  68. data/lib/java/processing/data/FloatList.java +936 -0
  69. data/lib/java/processing/data/IntDict.java +807 -0
  70. data/lib/java/processing/data/IntList.java +936 -0
  71. data/lib/java/processing/data/JSONArray.java +1260 -0
  72. data/lib/java/processing/data/JSONObject.java +2282 -0
  73. data/lib/java/processing/data/JSONTokener.java +435 -0
  74. data/lib/java/processing/data/LongDict.java +802 -0
  75. data/lib/java/processing/data/LongList.java +937 -0
  76. data/lib/java/processing/data/Sort.java +46 -0
  77. data/lib/java/processing/data/StringDict.java +613 -0
  78. data/lib/java/processing/data/StringList.java +800 -0
  79. data/lib/java/processing/data/Table.java +4936 -0
  80. data/lib/java/processing/data/TableRow.java +198 -0
  81. data/lib/java/processing/data/XML.java +1156 -0
  82. data/lib/java/processing/dxf/RawDXF.java +404 -0
  83. data/lib/java/processing/event/Event.java +125 -0
  84. data/lib/java/processing/event/KeyEvent.java +70 -0
  85. data/lib/java/processing/event/MouseEvent.java +114 -0
  86. data/lib/java/processing/event/TouchEvent.java +57 -0
  87. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  88. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  89. data/lib/java/processing/net/Client.java +744 -0
  90. data/lib/java/processing/net/Server.java +388 -0
  91. data/lib/java/processing/opengl/FontTexture.java +378 -0
  92. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  93. data/lib/java/processing/opengl/LinePath.java +627 -0
  94. data/lib/java/processing/opengl/LineStroker.java +681 -0
  95. data/lib/java/processing/opengl/PGL.java +3483 -0
  96. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  97. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  98. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  99. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  100. data/lib/java/processing/opengl/PShader.java +1484 -0
  101. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  102. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  103. data/lib/java/processing/opengl/Texture.java +1696 -0
  104. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  105. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  106. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  107. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  108. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  109. data/lib/java/processing/opengl/cursors/move.png +0 -0
  110. data/lib/java/processing/opengl/cursors/text.png +0 -0
  111. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  112. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  113. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  114. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  115. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  116. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  117. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  118. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  119. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  120. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  121. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  123. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  124. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  125. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  126. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  127. data/lib/propane/app.rb +9 -10
  128. data/lib/propane/runner.rb +10 -12
  129. data/lib/propane/version.rb +1 -1
  130. data/library/pdf/pdf.rb +7 -0
  131. data/library/svg/svg.rb +7 -0
  132. data/mvnw +3 -3
  133. data/mvnw.cmd +2 -2
  134. data/pom.rb +30 -3
  135. data/pom.xml +54 -3
  136. data/propane.gemspec +7 -3
  137. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  138. data/src/main/java/monkstone/MathToolModule.java +30 -30
  139. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  140. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  141. data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
  142. data/src/main/java/monkstone/fastmath/Deglut.java +6 -56
  143. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  144. data/src/main/java/monkstone/noise/OpenSimplex2F.java +813 -0
  145. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -0
  146. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  147. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  148. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  149. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  150. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  151. data/src/main/java/processing/core/PApplet.java +13242 -13374
  152. data/src/main/java/processing/core/PConstants.java +155 -163
  153. data/src/main/java/processing/core/PGraphics.java +118 -111
  154. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  155. data/src/main/java/processing/pdf/PGraphicsPDF.java +581 -0
  156. data/src/main/java/processing/svg/PGraphicsSVG.java +378 -0
  157. data/test/deglut_spec_test.rb +2 -2
  158. data/vendors/Rakefile +1 -1
  159. metadata +146 -17
  160. data/library/simplex_noise/simplex_noise.rb +0 -5
  161. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -436
@@ -0,0 +1,744 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+ /*
4
+ Client - basic network client implementation
5
+ Part of the Processing project - http://processing.org
6
+
7
+ Copyright (c) 2004-2007 Ben Fry and Casey Reas
8
+ The previous version of this code was developed by Hernando Barragan
9
+
10
+ This library is free software; you can redistribute it and/or
11
+ modify it under the terms of the GNU Lesser General Public
12
+ License as published by the Free Software Foundation; either
13
+ version 2.1 of the License, or (at your option) any later version.
14
+
15
+ This library is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ Lesser General Public License for more details.
19
+
20
+ You should have received a copy of the GNU Lesser General
21
+ Public License along with this library; if not, write to the
22
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23
+ Boston, MA 02111-1307 USA
24
+ */
25
+
26
+ package processing.net;
27
+ import processing.core.*;
28
+
29
+ import java.io.*;
30
+ import java.lang.reflect.*;
31
+ import java.net.*;
32
+
33
+ /**
34
+ * ( begin auto-generated from Client.xml )
35
+ *
36
+ * A client connects to a server and sends data back and forth. If anything
37
+ * goes wrong with the connection, for example the host is not there or is
38
+ * listening on a different port, an exception is thrown.
39
+ *
40
+ *
41
+ * @webref net
42
+ * @brief The client class is used to create client Objects which connect to a server to exchange data.
43
+ * @instanceName client any variable of type Client
44
+ * @usage Application
45
+ * @see_external LIB_net/clientEvent
46
+ */
47
+ public class Client implements Runnable {
48
+
49
+ protected static final int MAX_BUFFER_SIZE = 1 << 27; // 128 MB
50
+
51
+ PApplet parent;
52
+ Method clientEventMethod;
53
+ Method disconnectEventMethod;
54
+
55
+ volatile Thread thread;
56
+ Socket socket;
57
+ int port;
58
+ String host;
59
+
60
+ public InputStream input;
61
+ public OutputStream output;
62
+
63
+ final Object bufferLock = new Object[0];
64
+
65
+ byte buffer[] = new byte[32768];
66
+ int bufferIndex;
67
+ int bufferLast;
68
+
69
+ boolean disposeRegistered = false;
70
+
71
+
72
+ /**
73
+ * @param parent typically use "this"
74
+ * @param host address of the server
75
+ * @param port port to read/write from on the server
76
+ */
77
+ public Client(PApplet parent, String host, int port) {
78
+ this.parent = parent;
79
+ this.host = host;
80
+ this.port = port;
81
+
82
+ try {
83
+ socket = new Socket(this.host, this.port);
84
+ input = socket.getInputStream();
85
+ output = socket.getOutputStream();
86
+
87
+ thread = new Thread(this);
88
+ thread.start();
89
+
90
+ parent.registerMethod("dispose", this);
91
+ disposeRegistered = true;
92
+
93
+ // reflection to check whether host sketch has a call for
94
+ // public void clientEvent(processing.net.Client)
95
+ // which would be called each time an event comes in
96
+ try {
97
+ clientEventMethod =
98
+ parent.getClass().getMethod("clientEvent", Client.class);
99
+ } catch (Exception e) {
100
+ // no such method, or an error.. which is fine, just ignore
101
+ }
102
+ // do the same for disconnectEvent(Client c);
103
+ try {
104
+ disconnectEventMethod =
105
+ parent.getClass().getMethod("disconnectEvent", Client.class);
106
+ } catch (Exception e) {
107
+ // no such method, or an error.. which is fine, just ignore
108
+ }
109
+
110
+ } catch (IOException e) {
111
+
112
+ dispose();
113
+ }
114
+ }
115
+
116
+
117
+ /**
118
+ * @param socket any object of type Socket
119
+ * @throws IOException
120
+ */
121
+ public Client(PApplet parent, Socket socket) throws IOException {
122
+ this.parent = parent;
123
+ this.socket = socket;
124
+
125
+ input = socket.getInputStream();
126
+ output = socket.getOutputStream();
127
+
128
+ thread = new Thread(this);
129
+ thread.start();
130
+
131
+ // reflection to check whether host sketch has a call for
132
+ // public void clientEvent(processing.net.Client)
133
+ // which would be called each time an event comes in
134
+ try {
135
+ clientEventMethod =
136
+ parent.getClass().getMethod("clientEvent", Client.class);
137
+ } catch (Exception e) {
138
+ // no such method, or an error.. which is fine, just ignore
139
+ }
140
+ // do the same for disconnectEvent(Client c);
141
+ try {
142
+ disconnectEventMethod =
143
+ parent.getClass().getMethod("disconnectEvent", Client.class);
144
+ } catch (Exception e) {
145
+ // no such method, or an error.. which is fine, just ignore
146
+ }
147
+ }
148
+
149
+
150
+ /**
151
+ * ( begin auto-generated from Client_stop.xml )
152
+ *
153
+ * Disconnects from the server. Use to shut the connection when you're
154
+ * finished with the Client.
155
+ *
156
+ *
157
+ * @webref client:client
158
+ * @brief Disconnects from the server
159
+ * @usage application
160
+ */
161
+ public void stop() {
162
+ if (disconnectEventMethod != null && thread != null){
163
+ try {
164
+ disconnectEventMethod.invoke(parent, this);
165
+ } catch (Exception e) {
166
+ Throwable cause = e;
167
+ // unwrap the exception if it came from the user code
168
+ if (e instanceof InvocationTargetException && e.getCause() != null) {
169
+ cause = e.getCause();
170
+ }
171
+ cause.printStackTrace();
172
+ disconnectEventMethod = null;
173
+ }
174
+ }
175
+ if (disposeRegistered) {
176
+ parent.unregisterMethod("dispose", this);
177
+ disposeRegistered = false;
178
+ }
179
+ dispose();
180
+ }
181
+
182
+
183
+ /**
184
+ * Disconnect from the server: internal use only.
185
+ * <P>
186
+ * This should only be called by the internal functions in PApplet,
187
+ * use stop() instead from within your own applets.
188
+ */
189
+ public void dispose() {
190
+ thread = null;
191
+ try {
192
+ if (input != null) {
193
+ input.close();
194
+ input = null;
195
+ }
196
+ } catch (Exception e) {
197
+
198
+ }
199
+
200
+ try {
201
+ if (output != null) {
202
+ output.close();
203
+ output = null;
204
+ }
205
+ } catch (Exception e) {
206
+
207
+ }
208
+
209
+ try {
210
+ if (socket != null) {
211
+ socket.close();
212
+ socket = null;
213
+ }
214
+ } catch (Exception e) {
215
+
216
+ }
217
+ }
218
+
219
+
220
+ @Override
221
+ public void run() {
222
+ byte[] readBuffer;
223
+ { // make the read buffer same size as socket receive buffer so that
224
+ // we don't waste cycles calling listeners when there is more data waiting
225
+ int readBufferSize = 1 << 16; // 64 KB (default socket receive buffer size)
226
+ try {
227
+ readBufferSize = socket.getReceiveBufferSize();
228
+ } catch (SocketException ignore) { }
229
+ readBuffer = new byte[readBufferSize];
230
+ }
231
+ while (Thread.currentThread() == thread) {
232
+ try {
233
+ while (input != null) {
234
+ int readCount;
235
+
236
+ // try to read a byte using a blocking read.
237
+ // An exception will occur when the sketch is exits.
238
+ try {
239
+ readCount = input.read(readBuffer, 0, readBuffer.length);
240
+ } catch (SocketException e) {
241
+ System.err.println("Client SocketException: " + e.getMessage());
242
+ // the socket had a problem reading so don't try to read from it again.
243
+ stop();
244
+ return;
245
+ }
246
+
247
+ // read returns -1 if end-of-stream occurs (for example if the host disappears)
248
+ if (readCount == -1) {
249
+ System.err.println("Client got end-of-stream.");
250
+ stop();
251
+ return;
252
+ }
253
+
254
+ synchronized (bufferLock) {
255
+ int freeBack = buffer.length - bufferLast;
256
+ if (readCount > freeBack) {
257
+ // not enough space at the back
258
+ int bufferLength = bufferLast - bufferIndex;
259
+ byte[] targetBuffer = buffer;
260
+ if (bufferLength + readCount > buffer.length) {
261
+ // can't fit even after compacting, resize the buffer
262
+ // find the next power of two which can fit everything in
263
+ int newSize = Integer.highestOneBit(bufferLength + readCount - 1) << 1;
264
+ if (newSize > MAX_BUFFER_SIZE) {
265
+ // buffer is full because client is not reading (fast enough)
266
+ System.err.println("Client: can't receive more data, buffer is full. " +
267
+ "Make sure you read the data from the client.");
268
+ stop();
269
+ return;
270
+ }
271
+ targetBuffer = new byte[newSize];
272
+ }
273
+ // compact the buffer (either in-place or into the new bigger buffer)
274
+ System.arraycopy(buffer, bufferIndex, targetBuffer, 0, bufferLength);
275
+ bufferLast -= bufferIndex;
276
+ bufferIndex = 0;
277
+ buffer = targetBuffer;
278
+ }
279
+ // copy all newly read bytes into the buffer
280
+ System.arraycopy(readBuffer, 0, buffer, bufferLast, readCount);
281
+ bufferLast += readCount;
282
+ }
283
+
284
+ // now post an event
285
+ if (clientEventMethod != null) {
286
+ try {
287
+ clientEventMethod.invoke(parent, this);
288
+ } catch (Exception e) {
289
+ System.err.println("error, disabling clientEvent() for " + host);
290
+ Throwable cause = e;
291
+ // unwrap the exception if it came from the user code
292
+ if (e instanceof InvocationTargetException && e.getCause() != null) {
293
+ cause = e.getCause();
294
+ }
295
+ cause.printStackTrace();
296
+ clientEventMethod = null;
297
+ }
298
+ }
299
+ }
300
+ } catch (IOException e) {
301
+ //errorMessage("run", e);
302
+
303
+ }
304
+ }
305
+ }
306
+
307
+
308
+ /**
309
+ * ( begin auto-generated from Client_active.xml )
310
+ *
311
+ * Returns true if this client is still active and hasn't run
312
+ * into any trouble.
313
+ *
314
+ *
315
+ * @webref client:client
316
+ * @brief Returns true if this client is still active
317
+ * @usage application
318
+ */
319
+ public boolean active() {
320
+ return (thread != null);
321
+ }
322
+
323
+
324
+ /**
325
+ * ( begin auto-generated from Client_ip.xml )
326
+ *
327
+ * Returns the IP address of the computer to which the Client is attached.
328
+ *
329
+ *
330
+ * @webref client:client
331
+ * @usage application
332
+ * @brief Returns the IP address of the machine as a String
333
+ */
334
+ public String ip() {
335
+ if (socket != null){
336
+ return socket.getInetAddress().getHostAddress();
337
+ }
338
+ return null;
339
+ }
340
+
341
+
342
+ /**
343
+ * ( begin auto-generated from Client_available.xml )
344
+ *
345
+ * Returns the number of bytes available. When any client has bytes
346
+ * available from the server, it returns the number of bytes.
347
+ *
348
+ *
349
+ * @webref client:client
350
+ * @usage application
351
+ * @brief Returns the number of bytes in the buffer waiting to be read
352
+ */
353
+ public int available() {
354
+ synchronized (bufferLock) {
355
+ return (bufferLast - bufferIndex);
356
+ }
357
+ }
358
+
359
+
360
+ /**
361
+ * ( begin auto-generated from Client_clear.xml )
362
+ *
363
+ * Empty the buffer, removes all the data stored there.
364
+ *
365
+ *
366
+ * @webref client:client
367
+ * @usage application
368
+ * @brief Clears the buffer
369
+ */
370
+ public void clear() {
371
+ synchronized (bufferLock) {
372
+ bufferLast = 0;
373
+ bufferIndex = 0;
374
+ }
375
+ }
376
+
377
+
378
+ /**
379
+ * ( begin auto-generated from Client_read.xml )
380
+ *
381
+ * Returns a number between 0 and 255 for the next byte that's waiting in
382
+ * the buffer. Returns -1 if there is no byte, although this should be
383
+ * avoided by first cheacking <b>available()</b> to see if any data is available.
384
+ *
385
+ *
386
+ * @webref client:client
387
+ * @usage application
388
+ * @brief Returns a value from the buffer
389
+ */
390
+ public int read() {
391
+ synchronized (bufferLock) {
392
+ if (bufferIndex == bufferLast) return -1;
393
+
394
+ int outgoing = buffer[bufferIndex++] & 0xff;
395
+ if (bufferIndex == bufferLast) { // rewind
396
+ bufferIndex = 0;
397
+ bufferLast = 0;
398
+ }
399
+ return outgoing;
400
+ }
401
+ }
402
+
403
+
404
+ /**
405
+ * ( begin auto-generated from Client_readChar.xml )
406
+ *
407
+ * Returns the next byte in the buffer as a char. Returns -1 or 0xffff if
408
+ * nothing is there.
409
+ *
410
+ *
411
+ * @webref client:client
412
+ * @usage application
413
+ * @brief Returns the next byte in the buffer as a char
414
+ */
415
+ public char readChar() {
416
+ synchronized (bufferLock) {
417
+ if (bufferIndex == bufferLast) return (char) (-1);
418
+ return (char) read();
419
+ }
420
+ }
421
+
422
+
423
+ /**
424
+ * ( begin auto-generated from Client_readBytes.xml )
425
+ *
426
+ * Reads a group of bytes from the buffer. The version with no parameters
427
+ * returns a byte array of all data in the buffer. This is not efficient,
428
+ * but is easy to use. The version with the <b>byteBuffer</b> parameter is
429
+ * more memory and time efficient. It grabs the data in the buffer and puts
430
+ * it into the byte array passed in and returns an int value for the number
431
+ * of bytes read. If more bytes are available than can fit into the
432
+ * <b>byteBuffer</b>, only those that fit are read.
433
+ *
434
+ *
435
+ * <h3>Advanced</h3>
436
+ * Return a byte array of anything that's in the serial buffer.
437
+ * Not particularly memory/speed efficient, because it creates
438
+ * a byte array on each read, but it's easier to use than
439
+ * readBytes(byte b[]) (see below).
440
+ *
441
+ * @webref client:client
442
+ * @usage application
443
+ * @brief Reads everything in the buffer
444
+ */
445
+ public byte[] readBytes() {
446
+ synchronized (bufferLock) {
447
+ if (bufferIndex == bufferLast) return null;
448
+
449
+ int length = bufferLast - bufferIndex;
450
+ byte outgoing[] = new byte[length];
451
+ System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
452
+
453
+ bufferIndex = 0; // rewind
454
+ bufferLast = 0;
455
+ return outgoing;
456
+ }
457
+ }
458
+
459
+
460
+ /**
461
+ * <h3>Advanced</h3>
462
+ * Return a byte array of anything that's in the serial buffer
463
+ * up to the specified maximum number of bytes.
464
+ * Not particularly memory/speed efficient, because it creates
465
+ * a byte array on each read, but it's easier to use than
466
+ * readBytes(byte b[]) (see below).
467
+ *
468
+ * @param max the maximum number of bytes to read
469
+ */
470
+ public byte[] readBytes(int max) {
471
+ synchronized (bufferLock) {
472
+ if (bufferIndex == bufferLast) return null;
473
+
474
+ int length = bufferLast - bufferIndex;
475
+ if (length > max) length = max;
476
+ byte outgoing[] = new byte[length];
477
+ System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
478
+
479
+ bufferIndex += length;
480
+ if (bufferIndex == bufferLast) {
481
+ bufferIndex = 0; // rewind
482
+ bufferLast = 0;
483
+ }
484
+
485
+ return outgoing;
486
+ }
487
+ }
488
+
489
+
490
+ /**
491
+ * <h3>Advanced</h3>
492
+ * Grab whatever is in the serial buffer, and stuff it into a
493
+ * byte buffer passed in by the user. This is more memory/time
494
+ * efficient than readBytes() returning a byte[] array.
495
+ *
496
+ * Returns an int for how many bytes were read. If more bytes
497
+ * are available than can fit into the byte array, only those
498
+ * that will fit are read.
499
+ *
500
+ * @param bytebuffer passed in byte array to be altered
501
+ */
502
+ public int readBytes(byte bytebuffer[]) {
503
+ synchronized (bufferLock) {
504
+ if (bufferIndex == bufferLast) return 0;
505
+
506
+ int length = bufferLast - bufferIndex;
507
+ if (length > bytebuffer.length) length = bytebuffer.length;
508
+ System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length);
509
+
510
+ bufferIndex += length;
511
+ if (bufferIndex == bufferLast) {
512
+ bufferIndex = 0; // rewind
513
+ bufferLast = 0;
514
+ }
515
+ return length;
516
+ }
517
+ }
518
+
519
+
520
+ /**
521
+ * ( begin auto-generated from Client_readBytesUntil.xml )
522
+ *
523
+ * Reads from the port into a buffer of bytes up to and including a
524
+ * particular character. If the character isn't in the buffer, 'null' is
525
+ * returned. The version with no <b>byteBuffer</b> parameter returns a byte
526
+ * array of all data up to and including the <b>interesting</b> byte. This
527
+ * is not efficient, but is easy to use. The version with the
528
+ * <b>byteBuffer</b> parameter is more memory and time efficient. It grabs
529
+ * the data in the buffer and puts it into the byte array passed in and
530
+ * returns an int value for the number of bytes read. If the byte buffer is
531
+ * not large enough, -1 is returned and an error is printed to the message
532
+ * area. If nothing is in the buffer, 0 is returned.
533
+ *
534
+ *
535
+ * @webref client:client
536
+ * @usage application
537
+ * @brief Reads from the buffer of bytes up to and including a particular character
538
+ * @param interesting character designated to mark the end of the data
539
+ */
540
+ public byte[] readBytesUntil(int interesting) {
541
+ byte what = (byte)interesting;
542
+
543
+ synchronized (bufferLock) {
544
+ if (bufferIndex == bufferLast) return null;
545
+
546
+ int found = -1;
547
+ for (int k = bufferIndex; k < bufferLast; k++) {
548
+ if (buffer[k] == what) {
549
+ found = k;
550
+ break;
551
+ }
552
+ }
553
+ if (found == -1) return null;
554
+
555
+ int length = found - bufferIndex + 1;
556
+ byte outgoing[] = new byte[length];
557
+ System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
558
+
559
+ bufferIndex += length;
560
+ if (bufferIndex == bufferLast) {
561
+ bufferIndex = 0; // rewind
562
+ bufferLast = 0;
563
+ }
564
+ return outgoing;
565
+ }
566
+ }
567
+
568
+
569
+ /**
570
+ * <h3>Advanced</h3>
571
+ * Reads from the serial port into a buffer of bytes until a
572
+ * particular character. If the character isn't in the serial
573
+ * buffer, then 'null' is returned.
574
+ *
575
+ * If outgoing[] is not big enough, then -1 is returned,
576
+ * and an error message is printed on the console.
577
+ * If nothing is in the buffer, zero is returned.
578
+ * If 'interesting' byte is not in the buffer, then 0 is returned.
579
+ *
580
+ * @param byteBuffer passed in byte array to be altered
581
+ */
582
+ public int readBytesUntil(int interesting, byte byteBuffer[]) {
583
+ byte what = (byte)interesting;
584
+
585
+ synchronized (bufferLock) {
586
+ if (bufferIndex == bufferLast) return 0;
587
+
588
+ int found = -1;
589
+ for (int k = bufferIndex; k < bufferLast; k++) {
590
+ if (buffer[k] == what) {
591
+ found = k;
592
+ break;
593
+ }
594
+ }
595
+ if (found == -1) return 0;
596
+
597
+ int length = found - bufferIndex + 1;
598
+ if (length > byteBuffer.length) {
599
+ System.err.println("readBytesUntil() byte buffer is" +
600
+ " too small for the " + length +
601
+ " bytes up to and including char " + interesting);
602
+ return -1;
603
+ }
604
+ //byte outgoing[] = new byte[length];
605
+ System.arraycopy(buffer, bufferIndex, byteBuffer, 0, length);
606
+
607
+ bufferIndex += length;
608
+ if (bufferIndex == bufferLast) {
609
+ bufferIndex = 0; // rewind
610
+ bufferLast = 0;
611
+ }
612
+ return length;
613
+ }
614
+ }
615
+
616
+
617
+ /**
618
+ * ( begin auto-generated from Client_readString.xml )
619
+ *
620
+ * Returns the all the data from the buffer as a String. This method
621
+ * assumes the incoming characters are ASCII. If you want to transfer
622
+ * Unicode data, first convert the String to a byte stream in the
623
+ * representation of your choice (i.e. UTF8 or two-byte Unicode data), and
624
+ * send it as a byte array.
625
+ *
626
+ *
627
+ * @webref client:client
628
+ * @usage application
629
+ * @brief Returns the buffer as a String
630
+ */
631
+ public String readString() {
632
+ byte b[] = readBytes();
633
+ if (b == null) return null;
634
+ return new String(b);
635
+ }
636
+
637
+
638
+ /**
639
+ * ( begin auto-generated from Client_readStringUntil.xml )
640
+ *
641
+ * Combination of <b>readBytesUntil()</b> and <b>readString()</b>. Returns
642
+ * <b>null</b> if it doesn't find what you're looking for.
643
+ *
644
+ *
645
+ * <h3>Advanced</h3>
646
+ * <p/>
647
+ * If you want to move Unicode data, you can first convert the
648
+ * String to a byte stream in the representation of your choice
649
+ * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
650
+ *
651
+ * @webref client:client
652
+ * @usage application
653
+ * @brief Returns the buffer as a String up to and including a particular character
654
+ * @param interesting character designated to mark the end of the data
655
+ */
656
+ public String readStringUntil(int interesting) {
657
+ byte b[] = readBytesUntil(interesting);
658
+ if (b == null) return null;
659
+ return new String(b);
660
+ }
661
+
662
+
663
+ /**
664
+ * ( begin auto-generated from Client_write.xml )
665
+ *
666
+ * Writes data to a server specified when constructing the client.
667
+ *
668
+ *
669
+ * @webref client:client
670
+ * @usage application
671
+ * @brief Writes bytes, chars, ints, bytes[], Strings
672
+ * @param data data to write
673
+ */
674
+ public void write(int data) { // will also cover char
675
+ try {
676
+ output.write(data & 0xff); // for good measure do the &
677
+ output.flush(); // hmm, not sure if a good idea
678
+
679
+ } catch (Exception e) { // null pointer or serial port dead
680
+ //errorMessage("write", e);
681
+ //e.printStackTrace();
682
+ //dispose();
683
+ //disconnect(e);
684
+
685
+ stop();
686
+ }
687
+ }
688
+
689
+
690
+ public void write(byte data[]) {
691
+ try {
692
+ output.write(data);
693
+ output.flush(); // hmm, not sure if a good idea
694
+
695
+ } catch (Exception e) { // null pointer or serial port dead
696
+ //errorMessage("write", e);
697
+ //e.printStackTrace();
698
+ //disconnect(e);
699
+
700
+ stop();
701
+ }
702
+ }
703
+
704
+
705
+ /**
706
+ * <h3>Advanced</h3>
707
+ * Write a String to the output. Note that this doesn't account
708
+ * for Unicode (two bytes per char), nor will it send UTF8
709
+ * characters.. It assumes that you mean to send a byte buffer
710
+ * (most often the case for networking and serial i/o) and
711
+ * will only use the bottom 8 bits of each char in the string.
712
+ * (Meaning that internally it uses String.getBytes)
713
+ *
714
+ * If you want to move Unicode data, you can first convert the
715
+ * String to a byte stream in the representation of your choice
716
+ * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
717
+ */
718
+ public void write(String data) {
719
+ write(data.getBytes());
720
+ }
721
+
722
+
723
+ /**
724
+ * Handle disconnect due to an Exception being thrown.
725
+ */
726
+ /*
727
+ protected void disconnect(Exception e) {
728
+ dispose();
729
+ if (e != null) {
730
+ e.printStackTrace();
731
+ }
732
+ }
733
+ */
734
+
735
+
736
+ /**
737
+ * General error reporting, all corralled here just in case
738
+ * I think of something slightly more intelligent to do.
739
+ */
740
+ //public void errorMessage(String where, Exception e) {
741
+ //parent.die("Error inside Client." + where + "()", e);
742
+ //e.printStackTrace(System.err);
743
+ //}
744
+ }