001/* jpvmBuffer.java
002 *
003 * Classes to implement a packable / unpackable buffer that can
004 * be used to store the payload associated with a message.
005 *
006 * Adam J Ferrari
007 * Sun 05-26-1996
008 *
009 * Copyright (C) 1996  Adam J Ferrari
010 * 
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Library General Public
013 * License as published by the Free Software Foundation; either
014 * version 2 of the License, or (at your option) any later version.
015 * 
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019 * Library General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Library General Public
022 * License along with this library; if not, write to the
023 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
024 * MA 02139, USA.
025 */
026
027package jpvm;
028import jpvm.jpvmException;
029import jpvm.jpvmDataType;
030import jpvm.jpvmDebug;
031import jpvm.jpvmSendConnection;
032import jpvm.jpvmRecvConnection;
033import jpvm.jpvmTaskId;
034import java.io.*;
035import cnslab.cnsmath.*;
036import cnslab.cnsnetwork.*;
037
038class jpvmBufferElementContents implements Serializable {
039        public int              dataType;
040        public int              arraySize;
041        public byte             byteArray[];
042        public char             charArray[];
043        public int              intArray[];
044        public short            shortArray[];
045        public long             longArray[];
046        public float            floatArray[];
047        public double           doubleArray[];
048        public jpvmTaskId       taskArray[];
049        public Neuron           neuronArray[];
050        public Transmissible    objectArray[];
051
052        private void init() {
053                dataType        = jpvmDataType.jpvmNull;
054                arraySize       = 0;
055                byteArray       = null;
056                charArray       = null;
057                shortArray      = null;
058                intArray        = null;
059                longArray       = null;
060                floatArray      = null;
061                doubleArray     = null;
062                taskArray       = null;
063                neuronArray     = null;
064        }
065
066        public jpvmBufferElementContents(jpvmTaskId d[],int n,int stride,
067            boolean inPlace) {
068                init();
069                dataType = jpvmDataType.jpvmTid;
070                if(stride==1) {
071                        if(inPlace)
072                                taskArray=d;
073                        else {
074                                taskArray = new jpvmTaskId[n];
075                                System.arraycopy(d,0,taskArray,0,n);
076                        }
077                }
078                else {
079                        taskArray = new jpvmTaskId[n];
080                        int i,j;
081                        for(i=0, j=0; i<n; i++,j+=stride)
082                                taskArray[i] = d[j];
083                }
084        }
085
086        public jpvmBufferElementContents(Neuron d[],int n,int stride,
087            boolean inPlace) {
088                init();
089                dataType = jpvmDataType.jpvmNeuron;
090                if(stride==1) {
091                        if(inPlace)
092                                neuronArray=d;
093                        else {
094                                neuronArray = new Neuron[n];
095                                System.arraycopy(d,0,neuronArray,0,n);
096                        }
097                }
098                else {
099                        neuronArray = new Neuron[n];
100                        int i,j;
101                        for(i=0, j=0; i<n; i++,j+=stride)
102                                neuronArray[i] = d[j];
103                }
104        }
105
106        public jpvmBufferElementContents(Transmissible d[],int n,int stride,
107            boolean inPlace) {
108                init();
109                dataType = jpvmDataType.jpvmTransmissible;
110                if(stride==1) {
111                        if(inPlace)
112                                objectArray=d;
113                        else {
114                                objectArray = new Transmissible[n];
115                                System.arraycopy(d,0,objectArray,0,n);
116                        }
117                }
118                else {
119                        objectArray = new Transmissible[n];
120                        int i,j;
121                        for(i=0, j=0; i<n; i++,j+=stride)
122                                objectArray[i] = d[j];
123                }
124        }
125
126
127
128
129        public jpvmBufferElementContents(short d[],int n,int stride,
130            boolean inPlace) {
131                init();
132                dataType = jpvmDataType.jpvmShort;
133                if(stride==1) {
134                        if(inPlace)
135                                shortArray=d;
136                        else {
137                                shortArray = new short[n];
138                                System.arraycopy(d,0,shortArray,0,n);
139                        }
140                }
141                else {
142                        shortArray = new short[n];
143                        int i,j;
144                        for(i=0, j=0; i<n; i++,j+=stride)
145                                shortArray[i] = d[j];
146                }
147        }
148
149        public jpvmBufferElementContents(int d[],int n,int stride,
150            boolean inPlace) {
151                init();
152                dataType = jpvmDataType.jpvmInteger;
153                if(stride==1) {
154                        if(inPlace)
155                                intArray=d;
156                        else {
157                                intArray = new int[n];
158                                System.arraycopy(d,0,intArray,0,n);
159                        }
160                }
161                else {
162                        intArray = new int[n];
163                        int i,j;
164                        for(i=0, j=0; i<n; i++,j+=stride)
165                                intArray[i] = d[j];
166                }
167        }
168
169        public jpvmBufferElementContents(long d[],int n,int stride,
170            boolean inPlace) {
171                init();
172                dataType = jpvmDataType.jpvmLong;
173                if(stride==1) {
174                        if(inPlace)
175                                longArray=d;
176                        else {
177                                longArray = new long[n];
178                                System.arraycopy(d,0,longArray,0,n);
179                        }
180                }
181                else {
182                        longArray = new long[n];
183                        int i,j;
184                        for(i=0, j=0; i<n; i++,j+=stride)
185                                longArray[i] = d[j];
186                }
187        }
188
189        public jpvmBufferElementContents(char d[],int n,int stride,
190            boolean inPlace) {
191                init();
192                dataType = jpvmDataType.jpvmChar;
193                if(stride==1) {
194                        if(inPlace)
195                                charArray=d;
196                        else {
197                                charArray = new char[n];
198                                System.arraycopy(d,0,charArray,0,n);
199                        }
200                }
201                else {
202                        charArray = new char[n];
203                        int i,j;
204                        for(i=0, j=0; i<n; i++,j+=stride)
205                                charArray[i] = d[j];
206                }
207        }
208
209        public jpvmBufferElementContents(float d[],int n,int stride,
210            boolean inPlace) {
211                init();
212                dataType = jpvmDataType.jpvmFloat;
213                if(stride==1) {
214                        if(inPlace)
215                                floatArray=d;
216                        else {
217                                floatArray = new float[n];
218                                System.arraycopy(d,0,floatArray,0,n);
219                        }
220                }
221                else {
222                        floatArray = new float[n];
223                        int i,j;
224                        for(i=0, j=0; i<n; i++,j+=stride)
225                                floatArray[i] = d[j];
226                }
227        }
228
229        public jpvmBufferElementContents(double d[],int n,int stride,
230            boolean inPlace) {
231                init();
232                dataType = jpvmDataType.jpvmDouble;
233                if(stride==1) {
234                        if(inPlace)
235                                doubleArray=d;
236                        else {
237                                doubleArray = new double[n];
238                                System.arraycopy(d,0,doubleArray,0,n);
239                        }
240                }
241                else {
242                        doubleArray = new double[n];
243                        int i,j;
244                        for(i=0, j=0; i<n; i++,j+=stride)
245                                doubleArray[i] = d[j];
246                }
247        }
248
249        public jpvmBufferElementContents(byte d[],int n,int stride,
250            boolean inPlace) {
251                init();
252                dataType = jpvmDataType.jpvmByte;
253                if(stride==1) {
254                        if(inPlace)
255                                byteArray=d;
256                        else {
257                                byteArray = new byte[n];
258                                System.arraycopy(d,0,byteArray,0,n);
259                        }
260                }
261                else {
262                        byteArray = new byte[n];
263                        int i,j;
264                        for(i=0, j=0; i<n; i++,j+=stride)
265                                byteArray[i] = d[j];
266                }
267        }
268
269        public void unpack(int d[], int n, int stride) 
270            throws jpvmException {
271                if(dataType != jpvmDataType.jpvmInteger) {
272                    throw new jpvmException("buffer type mismatch, upkint.");
273                }
274                if(stride==1) {
275                        System.arraycopy(intArray,0,d,0,n);
276                }
277                else {
278                        int i,j;
279                        for(i=0,j=0;i<n;i++,j+=stride)
280                                d[j] = intArray[i];
281                }
282        }
283
284        public void unpack(short d[], int n, int stride) throws jpvmException {
285                if(dataType != jpvmDataType.jpvmShort) {
286                    throw new jpvmException("buffer type mismatch, upkshort.");
287                }
288                if(stride==1) {
289                        System.arraycopy(shortArray,0,d,0,n);
290                }
291                else {
292                        int i,j;
293                        for(i=0,j=0;i<n;i++,j+=stride)
294                                d[j] = shortArray[i];
295                }
296        }
297
298        public void unpack(byte d[], int n, int stride) throws jpvmException {
299                if(dataType != jpvmDataType.jpvmByte) {
300                    throw new jpvmException("buffer type mismatch, upkbyte.");
301                }
302                if(stride==1) 
303                        System.arraycopy(byteArray,0,d,0,n);
304                else {
305                        int i,j;
306                        for(i=0,j=0;i<n;i++,j+=stride)
307                                d[j] = byteArray[i];
308                }
309        }
310
311        public void unpack(char d[], int n, int stride) throws jpvmException {
312                if(dataType != jpvmDataType.jpvmChar) {
313                    throw new jpvmException("buffer type mismatch, upkchar.");
314                }
315                if(stride==1) {
316                        System.arraycopy(charArray,0,d,0,n);
317                }
318                else {
319                        int i,j;
320                        for(i=0,j=0;i<n;i++,j+=stride)
321                                d[j] = charArray[i];
322                }
323        }
324
325        public void unpack(long d[], int n, int stride) throws jpvmException {
326                if(dataType != jpvmDataType.jpvmLong) {
327                    throw new jpvmException("buffer type mismatch, upklong.");
328                }
329                if(stride==1) {
330                        System.arraycopy(longArray,0,d,0,n);
331                }
332                else {
333                        int i,j;
334                        for(i=0,j=0;i<n;i++,j+=stride)
335                                d[j] = longArray[i];
336                }
337        }
338
339        public void unpack(double d[], int n, int stride) throws jpvmException {
340                if(dataType != jpvmDataType.jpvmDouble) {
341                    throw new jpvmException("buffer type mismatch, upkdouble.");
342                }
343                if(stride==1) {
344                        System.arraycopy(doubleArray,0,d,0,n);
345                }
346                else {
347                        int i,j;
348                        for(i=0,j=0;i<n;i++,j+=stride)
349                                d[j] = doubleArray[i];
350                }
351        }
352
353        public void unpack(float d[], int n, int stride) throws jpvmException {
354                if(dataType != jpvmDataType.jpvmFloat) {
355                    throw new jpvmException("buffer type mismatch, upkfloat.");
356                }
357                if(stride==1) {
358                        System.arraycopy(floatArray,0,d,0,n);
359                }
360                else {
361                        int i,j;
362                        for(i=0,j=0;i<n;i++,j+=stride)
363                                d[j] = floatArray[i];
364                }
365        }
366
367        public void unpack(jpvmTaskId d[], int n, int stride)
368            throws jpvmException {
369                if(dataType != jpvmDataType.jpvmTid) {
370                    throw new jpvmException("buffer type mismatch, upktid.");
371                }
372                if(stride==1) {
373                        System.arraycopy(taskArray,0,d,0,n);
374                }
375                else {
376                        int i,j;
377                        for(i=0,j=0;i<n;i++,j+=stride)
378                                d[j] = taskArray[i];
379                }
380        }
381
382        public void unpack(Neuron d[], int n, int stride)
383            throws jpvmException {
384                if(dataType != jpvmDataType.jpvmNeuron) {
385                    throw new jpvmException("buffer type mismatch, upktid.");
386                }
387                if(stride==1) {
388                        System.arraycopy(neuronArray,0,d,0,n);
389                }
390                else {
391                        int i,j;
392                        for(i=0,j=0;i<n;i++,j+=stride)
393                                d[j] = neuronArray[i];
394                }
395        }
396
397        public void unpack(Transmissible d[], int n, int stride)
398            throws jpvmException {
399                if(dataType != jpvmDataType.jpvmTransmissible) {
400                    throw new jpvmException("buffer type mismatch, upktid.");
401                }
402                if(stride==1) {
403                        System.arraycopy(objectArray,0,d,0,n);
404                }
405                else {
406                        int i,j;
407                        for(i=0,j=0;i<n;i++,j+=stride)
408                                d[j] = objectArray[i];
409                }
410        }
411
412
413        public String unpack() throws jpvmException {
414                if(dataType != jpvmDataType.jpvmString) {
415                    throw new jpvmException("buffer type mismatch, upkstring.");
416                }
417                return new String(charArray);
418        }
419};
420
421class jpvmBufferElement {
422        private boolean                   inPlace;
423        public  jpvmBufferElementContents contents;
424        public  jpvmBufferElement         next; // Linked structure
425
426        public void init() {
427                contents = null;
428                next = null;
429        }
430
431        public jpvmBufferElement() {
432                init();
433                inPlace = false;
434        }
435
436        public jpvmBufferElement(boolean dataInPlace) {
437                init();
438                inPlace = dataInPlace;
439        }
440
441        public jpvmBufferElement(jpvmTaskId d[],int n,int stride) {
442                init();
443                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
444        }
445
446        public jpvmBufferElement(jpvmTaskId d) {
447                init();
448                jpvmTaskId a[] = new jpvmTaskId[1];
449                a[0] = d;
450                contents = new jpvmBufferElementContents(a,1,1,true);
451        }
452
453        public jpvmBufferElement(Neuron d[],int n,int stride) {
454                init();
455                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
456        }
457
458        public jpvmBufferElement(Neuron d) {
459                init();
460                Neuron a[] = new Neuron[1];
461                a[0] = d;
462                contents = new jpvmBufferElementContents(a,1,1,true);
463        }
464
465        public jpvmBufferElement(Transmissible d[],int n,int stride) {
466                init();
467                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
468        }
469
470        public jpvmBufferElement(Transmissible d) {
471                init();
472                Transmissible a[] = new Transmissible[1];
473                a[0] = d;
474                contents = new jpvmBufferElementContents(a,1,1,true);
475        }
476        public jpvmBufferElement(byte d[],int n,int stride) {
477                init();
478                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
479        }
480
481        public jpvmBufferElement(byte d) {
482                init();
483                byte a[] = new byte[1];
484                a[0] = d;
485                contents = new jpvmBufferElementContents(a,1,1,true);
486        }
487
488        public jpvmBufferElement(short d[],int n,int stride) {
489                init();
490                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
491        }
492
493        public jpvmBufferElement(short d) {
494                init();
495                short a[] = new short[1];
496                a[0] = d;
497                contents = new jpvmBufferElementContents(a,1,1,true);
498        }
499
500        public jpvmBufferElement(char d[],int n,int stride) {
501                init();
502                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
503        }
504
505        public jpvmBufferElement(char d) {
506                init();
507                char a[] = new char[1];
508                a[0] = d;
509                contents = new jpvmBufferElementContents(a,1,1,true);
510        }
511
512
513        public jpvmBufferElement(long d[],int n,int stride) {
514                init();
515                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
516        }
517
518        public jpvmBufferElement(long d) {
519                init();
520                long a[] = new long[1];
521                a[0] = d;
522                contents = new jpvmBufferElementContents(a,1,1,true);
523        }
524        
525        
526        public jpvmBufferElement(int d[],int n,int stride) {
527                init();
528                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
529        }
530
531        public jpvmBufferElement(int d) {
532                init();
533                int a[] = new int[1];
534                a[0] = d;
535                contents = new jpvmBufferElementContents(a,1,1,true);
536        }
537
538        public jpvmBufferElement(float d[],int n,int stride) {
539                init();
540                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
541        }
542
543        public jpvmBufferElement(float d) {
544                init();
545                float a[] = new float[1];
546                a[0] = d;
547                contents = new jpvmBufferElementContents(a,1,1,true);
548        }
549
550        public jpvmBufferElement(double d[],int n,int stride) {
551                init();
552                contents = new jpvmBufferElementContents(d,n,stride,inPlace);
553        }
554
555        public jpvmBufferElement(double d) {
556                init();
557                double a[] = new double[1];
558                a[0] = d;
559                contents = new jpvmBufferElementContents(a,1,1,true);
560        }
561
562        public jpvmBufferElement(String str) {
563                init();
564                int n = str.length();
565                char a[] = new char[n];
566                str.getChars(0,n,a,0);
567                contents = new jpvmBufferElementContents(a,n,1,true);
568                contents.dataType = jpvmDataType.jpvmString;
569        }
570
571        public void unpack(int d[], int n, int stride) throws jpvmException {
572                contents.unpack(d,n,stride);
573        }
574
575        public void unpack(short d[], int n, int stride) throws jpvmException {
576                contents.unpack(d,n,stride);
577        }
578
579        public void unpack(byte d[], int n, int stride) throws jpvmException {
580                contents.unpack(d,n,stride);
581        }
582
583        public void unpack(char d[], int n, int stride) throws jpvmException {
584                contents.unpack(d,n,stride);
585        }
586
587        public void unpack(long d[], int n, int stride) throws jpvmException {
588                contents.unpack(d,n,stride);
589        }
590
591        public void unpack(double d[], int n, int stride) throws jpvmException {
592                contents.unpack(d,n,stride);
593        }
594
595        public void unpack(float d[], int n, int stride) throws jpvmException {
596                contents.unpack(d,n,stride);
597        }
598
599        public void unpack(jpvmTaskId d[], int n, int stride)
600            throws jpvmException {
601                contents.unpack(d,n,stride);
602        }
603
604        public void unpack(Neuron d[], int n, int stride)
605            throws jpvmException {
606                contents.unpack(d,n,stride);
607        }
608
609        public void unpack(Transmissible d[], int n, int stride)
610            throws jpvmException {
611                contents.unpack(d,n,stride);
612        }
613
614
615        public String unpack() throws jpvmException {
616                return contents.unpack();
617        }
618
619        public void send(jpvmSendConnection conn) throws jpvmException {
620                int i;
621                try {
622                        ObjectOutputStream out;
623                        out = new ObjectOutputStream(conn.strm);
624                        out.writeObject(contents);
625                        //out.flush();
626                }
627                catch (IOException ioe) {
628                        System.err.println("I/O exception - "+ioe);
629                        jpvmDebug.note("jpvmBufferElement, " +
630                                "send - i/o exception");
631                        throw new jpvmException("jpvmBufferElement, " +
632                                "send - i/o exception");
633                }
634        }
635
636        public void recv(jpvmRecvConnection conn) throws jpvmException {
637                int i;
638                try {
639                        ObjectInputStream in;
640                        in = new ObjectInputStream(conn.strm);
641                        try {
642                          contents = (jpvmBufferElementContents)in.readObject();
643                        }
644                        catch (ClassNotFoundException cnf) {
645                          throw new jpvmException("jpvmBufferElement, " +
646                                "recv - can't find class "+
647                                "jpvmBufferElementContents");
648                        }
649                }
650                catch (IOException ioe) {
651                        jpvmDebug.note("jpvmBufferElement, " +
652                                "recv - i/o exception");
653                        throw new jpvmException("jpvmBufferElement, " +
654                                "recv - i/o exception");
655                }
656        }
657};
658
659public
660class jpvmBuffer {
661        private jpvmBufferElement list_head;
662        private jpvmBufferElement list_tail;
663        private jpvmBufferElement curr_elt;
664        private int num_list_elts;
665
666        private void addElt(jpvmBufferElement nw) {
667                num_list_elts ++;
668                if(list_head == null) {
669                        curr_elt = list_head = list_tail = nw;
670                        return;
671                }
672                list_tail.next = nw;
673                list_tail = nw;
674        }
675
676        public jpvmBuffer() {
677                list_head = null;
678                list_tail = null;
679                num_list_elts = 0;
680        }
681
682        public void rewind() {
683                curr_elt = list_head;
684        }
685
686        public void pack(int d[], int n, int stride) {
687                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
688                addElt(nw);
689        }
690
691        public void pack(int d) {
692                jpvmBufferElement nw = new jpvmBufferElement(d);
693                addElt(nw);
694        }
695
696        public void pack(char d[], int n, int stride) {
697                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
698                addElt(nw);
699        }
700
701        public void pack(char d) {
702                jpvmBufferElement nw = new jpvmBufferElement(d);
703                addElt(nw);
704        }
705
706        public void pack(short d[], int n, int stride) {
707                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
708                addElt(nw);
709        }
710
711        public void pack(short d) {
712                jpvmBufferElement nw = new jpvmBufferElement(d);
713                addElt(nw);
714        }
715
716        public void pack(long d[], int n, int stride) {
717                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
718                addElt(nw);
719        }
720
721        public void pack(long d) {
722                jpvmBufferElement nw = new jpvmBufferElement(d);
723                addElt(nw);
724        }
725
726        public void pack(byte d[], int n, int stride) {
727                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
728                addElt(nw);
729        }
730
731        public void pack(byte d) {
732                jpvmBufferElement nw = new jpvmBufferElement(d);
733                addElt(nw);
734        }
735
736        public void pack(float d[], int n, int stride) {
737                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
738                addElt(nw);
739        }
740
741        public void pack(float d) {
742                jpvmBufferElement nw = new jpvmBufferElement(d);
743                addElt(nw);
744        }
745
746        public void pack(double d[], int n, int stride) {
747                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
748                addElt(nw);
749        }
750
751        public void pack(double d) {
752                jpvmBufferElement nw = new jpvmBufferElement(d);
753                addElt(nw);
754        }
755
756        public void pack(jpvmTaskId d[], int n, int stride) {
757                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
758                addElt(nw);
759        }
760
761        public void pack(jpvmTaskId d) {
762                jpvmBufferElement nw = new jpvmBufferElement(d);
763                addElt(nw);
764        }
765
766        public void pack(Neuron d[], int n, int stride) {
767                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
768                addElt(nw);
769        }
770
771        public void pack(Neuron d) {
772                jpvmBufferElement nw = new jpvmBufferElement(d);
773                addElt(nw);
774        }
775
776        public void pack(Transmissible d[], int n, int stride) {
777                jpvmBufferElement nw = new jpvmBufferElement(d,n,stride);
778                addElt(nw);
779        }
780
781        public void pack(Transmissible d) {
782                jpvmBufferElement nw = new jpvmBufferElement(d);
783                addElt(nw);
784        }
785
786
787
788        public void pack(String str) {
789                jpvmBufferElement nw = new jpvmBufferElement(str);
790                addElt(nw);
791        }
792
793        public void unpack(int d[], int n, int stride) throws jpvmException {
794                if(curr_elt == null)
795                        throw new jpvmException("buffer empty, upkint.");
796                curr_elt.unpack(d,n,stride);
797                curr_elt = curr_elt.next;
798        }
799        
800        public int upkint() throws jpvmException {
801                int d[] = new int[1];
802                unpack(d,1,1);
803                return d[0];
804        }
805
806        public void unpack(byte d[], int n, int stride) throws jpvmException {
807                if(curr_elt == null)
808                        throw new jpvmException("buffer empty, upkbyte.");
809                curr_elt.unpack(d,n,stride);
810                curr_elt = curr_elt.next;
811        }
812
813        public byte upkbyte() throws jpvmException {
814                byte d[] = new byte[1];
815                unpack(d,1,1);
816                return d[0];
817        }
818
819        public void unpack(char d[], int n, int stride) throws jpvmException {
820                if(curr_elt == null)
821                        throw new jpvmException("buffer empty, upkchar.");
822                curr_elt.unpack(d,n,stride);
823                curr_elt = curr_elt.next;
824        }
825
826        public char upkchar() throws jpvmException {
827                char d[] = new char[1];
828                unpack(d,1,1);
829                return d[0];
830        }
831
832        public void unpack(short d[], int n, int stride) throws jpvmException {
833                if(curr_elt == null)
834                        throw new jpvmException("buffer empty, upkshort.");
835                curr_elt.unpack(d,n,stride);
836                curr_elt = curr_elt.next;
837        }
838
839        public short upkshort() throws jpvmException {
840                short d[] = new short[1];
841                unpack(d,1,1);
842                return d[0];
843        }
844
845        public void unpack(long d[], int n, int stride) throws jpvmException {
846                if(curr_elt == null)
847                        throw new jpvmException("buffer empty, upklong.");
848                curr_elt.unpack(d,n,stride);
849                curr_elt = curr_elt.next;
850        }
851
852        public long upklong() throws jpvmException {
853                long d[] = new long[1];
854                unpack(d,1,1);
855                return d[0];
856        }
857
858        public void unpack(float d[], int n, int stride) throws jpvmException {
859                if(curr_elt == null)
860                        throw new jpvmException("buffer empty, upkfloat.");
861                curr_elt.unpack(d,n,stride);
862                curr_elt = curr_elt.next;
863        }
864
865        public float upkfloat() throws jpvmException {
866                float d[] = new float[1];
867                unpack(d,1,1);
868                return d[0];
869        }
870
871        public void unpack(double d[], int n, int stride) throws jpvmException {
872                if(curr_elt == null)
873                        throw new jpvmException("buffer empty, upkdouble.");
874                curr_elt.unpack(d,n,stride);
875                curr_elt = curr_elt.next;
876        }
877
878        public double upkdouble() throws jpvmException {
879                double d[] = new double[1];
880                unpack(d,1,1);
881                return d[0];
882        }
883
884        public void unpack(jpvmTaskId d[], int n, int stride) 
885          throws jpvmException {
886                if(curr_elt == null)
887                        throw new jpvmException("buffer empty, upktid.");
888                curr_elt.unpack(d,n,stride);
889                curr_elt = curr_elt.next;
890        }
891
892        public jpvmTaskId upktid() throws jpvmException {
893                jpvmTaskId d[] = new jpvmTaskId[1];
894                unpack(d,1,1);
895                return d[0];
896        }
897
898        public void unpack(Neuron d[], int n, int stride) 
899          throws jpvmException {
900                if(curr_elt == null)
901                        throw new jpvmException("buffer empty, upktid.");
902                curr_elt.unpack(d,n,stride);
903                curr_elt = curr_elt.next;
904        }
905
906        public Neuron upkneuron() throws jpvmException {
907                Neuron d[] = new Neuron[1];
908                unpack(d,1,1);
909                return d[0];
910        }
911
912        public void unpack(Transmissible d[], int n, int stride) 
913          throws jpvmException {
914                if(curr_elt == null)
915                        throw new jpvmException("buffer empty, upktid.");
916                curr_elt.unpack(d,n,stride);
917                curr_elt = curr_elt.next;
918        }
919
920        public Transmissible upkcnsobj() throws jpvmException {
921                Transmissible d[] = new Transmissible[1];
922                unpack(d,1,1);
923                return d[0];
924        }
925
926
927        public String upkstr() throws jpvmException {
928                if(curr_elt == null)
929                        throw new jpvmException("buffer empty, upkstring.");
930                String ret = curr_elt.unpack();
931                curr_elt = curr_elt.next;
932                return ret;
933        }
934
935        public void send(jpvmSendConnection conn) throws jpvmException {
936                DataOutputStream strm = conn.strm;
937                jpvmBufferElement tmp = list_head;
938                try {
939                        strm.writeInt(num_list_elts);
940                        while(tmp != null) {
941                                tmp.send(conn);
942                                tmp = tmp.next;
943                        }
944                }
945                catch (IOException ioe) {
946                        jpvmDebug.note("jpvmBuffer, send - i/o exception");
947                        throw new jpvmException("jpvmBuffer, send - " +
948                                "i/o exception");
949                }
950        }
951
952        public void recv(jpvmRecvConnection conn) throws jpvmException {
953                int i, N;
954                jpvmBufferElement tmp;
955                
956                DataInputStream strm = conn.strm;
957                try {
958                        N = strm.readInt();
959                        jpvmDebug.note("jpvmBuffer, recv "+N+
960                                " buffer elements.");
961                        for(i=0;i<N;i++) {
962                                tmp = new jpvmBufferElement();
963                                tmp.recv(conn);
964                                addElt(tmp);
965                        }
966                }
967                catch (IOException ioe) {
968                        jpvmDebug.note("jpvmBuffer, recv - i/o exception");
969                        throw new jpvmException("jpvmBuffer, recv - " +
970                                "i/o exception");
971                }
972        }
973};