com/scalagent/kjoram/BytesMessage.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - ScalAgent Distributed Technologies
00004  * Copyright (C) 1996 - Dyade
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA.
00020  *
00021  * Initial developer(s): Frederic Maistre (INRIA)
00022  * Contributor(s): Nicolas Tachker (ScalAgent)
00023  */
00024 package com.scalagent.kjoram;
00025 
00026 import java.io.*;
00027 
00028 import com.scalagent.kjoram.excepts.JMSException;
00029 import com.scalagent.kjoram.excepts.MessageFormatException;
00030 import com.scalagent.kjoram.excepts.MessageNotWriteableException;
00031 import com.scalagent.kjoram.excepts.MessageNotReadableException;
00032 import com.scalagent.kjoram.excepts.MessageEOFException;
00033 
00034 
00035 public class BytesMessage extends Message
00036 {
00038   private ByteArrayOutputStream outputBuffer = null;
00040   private DataOutputStream outputStream = null;
00042   private DataInputStream inputStream = null;
00043 
00045   private boolean RObody = false;
00047   private boolean WObody = true;
00048 
00050   private byte[] bytes = null;
00052   private boolean prepared = false;
00053 
00054 
00058   BytesMessage()
00059   {
00060     super();
00061     outputBuffer = new ByteArrayOutputStream();
00062     outputStream = new DataOutputStream(outputBuffer);
00063   }
00064 
00072   BytesMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg)
00073   {
00074     super(sess, momMsg);
00075     bytes = momMsg.getBytes();
00076 
00077     inputStream = new DataInputStream(new ByteArrayInputStream(bytes));
00078 
00079     RObody = true;
00080     WObody = false;
00081   }
00082  
00083 
00089   public long getBodyLength() throws JMSException
00090   {
00091     if (WObody)
00092       throw new MessageNotReadableException("Can't get not readable message's"
00093                                             + " size.");
00094     return bytes.length;
00095   } 
00096 
00103   public void clearBody() throws JMSException
00104   {
00105     super.clearBody();
00106 
00107     try {
00108       if (WObody) {
00109         outputStream.close();
00110         outputBuffer.close();
00111       }
00112       else
00113         inputStream.close();
00114 
00115       outputBuffer = new ByteArrayOutputStream();
00116       outputStream = new DataOutputStream(outputBuffer);
00117       bytes = null;
00118       RObody = false;
00119       WObody = true;
00120       prepared = false;
00121     }
00122     catch (IOException ioE) {
00123       JMSException jE = new JMSException("Error while closing the stream"
00124                                          + " facilities.");
00125       jE.setLinkedException(ioE);
00126       throw jE;
00127     }
00128   }
00129 
00130 
00137   public void writeBoolean(boolean value) throws JMSException
00138   {
00139     writeObject(new Boolean(value));
00140   }
00141  
00148   public void writeByte(byte value) throws JMSException
00149   {
00150     writeObject(new Byte(value));
00151   }
00152  
00159   public void writeBytes(byte[] value) throws JMSException
00160   {
00161     writeObject(value);
00162   }
00163 
00170   public void writeBytes(byte[] value, int offset, int length)
00171               throws JMSException
00172   {
00173     if (RObody)
00174       throw new MessageNotWriteableException("Can't write a value as the"
00175                                              + " message body is read-only.");
00176 
00177     if (prepared) {
00178       prepared = false;
00179       outputBuffer = new ByteArrayOutputStream();
00180       outputStream = new DataOutputStream(outputBuffer);
00181     }
00182 
00183     try {
00184       outputStream.write(value, offset, length);
00185     }
00186     catch (IOException ioE) {
00187       JMSException jE = new JMSException("Error while writing the value.");
00188       jE.setLinkedException(ioE);
00189       throw jE;
00190     }
00191   }
00192  
00199   public void writeChar(char value) throws JMSException
00200   {
00201     writeObject(new Character(value));
00202   }
00203  
00210 //    public void writeDouble(double value) throws JMSException
00211 //    {
00212 //      writeObject(new Double(value));
00213 //    }
00214  
00215 //    /** 
00216 //     * API method.
00217 //     *
00218 //     * @exception MessageNotWriteableException  If the message body is read-only.
00219 //     * @exception JMSException  If the value could not be written on the stream.
00220 //     */   
00221 //    public void writeFloat(float value) throws JMSException
00222 //    {
00223 //      writeObject(new Float(value));
00224 //    }
00225  
00232   public void writeInt(int value) throws JMSException
00233   {
00234     writeObject(new Integer(value));
00235   }
00236  
00243   public void writeLong(long value) throws JMSException
00244   {
00245     writeObject(new Long(value));
00246   }
00247 
00254   public void writeShort(short value) throws JMSException
00255   {
00256     writeObject(new Short(value));
00257   }
00258  
00265   public void writeUTF(String value) throws JMSException
00266   {
00267     writeObject(value);
00268   }
00269 
00277   public void writeObject(Object value) throws JMSException
00278   {
00279     if (RObody)
00280       throw new MessageNotWriteableException("Can't write a value as the"
00281                                              + " message body is read-only.");
00282 
00283     if (value == null)
00284       throw new NullPointerException("Forbidden null value.");
00285 
00286     if (prepared) {
00287       prepared = false;
00288       outputBuffer = new ByteArrayOutputStream();
00289       outputStream = new DataOutputStream(outputBuffer);
00290     }
00291 
00292     try {
00293       if (value instanceof Boolean)
00294         outputStream.writeBoolean(((Boolean) value).booleanValue());
00295       else if (value instanceof Character)
00296         outputStream.writeChar(((Character) value).charValue());
00297       else if (value instanceof Integer)
00298         outputStream.writeInt(((Integer) value).intValue());
00299       else if (value instanceof Long)
00300         outputStream.writeLong(((Long) value).longValue());
00301 //        else if (value instanceof Float)
00302 //          outputStream.writeFloat(((Float) value).floatValue());
00303 //        else if (value instanceof Double)
00304 //          outputStream.writeDouble(((Double) value).doubleValue());
00305       else if (value instanceof String)
00306         outputStream.writeUTF((String) value);
00307       else if (value instanceof byte[])
00308         outputStream.write((byte[]) value);
00309       else
00310         throw new MessageFormatException("Can't write non Java primitive type"
00311                                          + " as a bytes array.");
00312     }
00313     catch (IOException ioE) {
00314       JMSException jE = new JMSException("Error while writing the value.");
00315       jE.setLinkedException(ioE);
00316       throw jE;
00317     }
00318   }
00319   
00320   
00327   public boolean readBoolean() throws JMSException
00328   {
00329     if (WObody)
00330       throw new MessageNotReadableException("Can't read the message body as"
00331                                             + " it is write-only.");
00332     try {
00333       return inputStream.readBoolean();
00334     }
00335     catch (Exception e) {
00336       JMSException jE = null;
00337       if (e instanceof EOFException)
00338         jE = new MessageEOFException("Unexpected end of bytes array.");
00339       else if (e instanceof IOException)
00340         jE = new JMSException("Could not read the bytes array.");
00341       jE.setLinkedException(e);
00342       throw jE;
00343     }
00344   }
00345 
00352   public byte readByte() throws JMSException
00353   {
00354     if (WObody)
00355       throw new MessageNotReadableException("Can't read the message body as"
00356                                             + " it is write-only.");
00357     try {
00358       return inputStream.readByte();
00359     }
00360     catch (Exception e) {
00361       JMSException jE = null;
00362       if (e instanceof EOFException)
00363         jE = new MessageEOFException("Unexpected end of bytes array.");
00364       else if (e instanceof IOException)
00365         jE = new JMSException("Could not read the bytes array.");
00366       jE.setLinkedException(e);
00367       throw jE;
00368     }
00369   }
00370 
00377   public int readUnsignedByte() throws JMSException
00378   {
00379     if (WObody)
00380       throw new MessageNotReadableException("Can't read the message body as"
00381                                             + " it is write-only.");
00382     try {
00383       return inputStream.readUnsignedByte();
00384     }
00385     catch (Exception e) {
00386       JMSException jE = null;
00387       if (e instanceof EOFException)
00388         jE = new MessageEOFException("Unexpected end of bytes array.");
00389       else if (e instanceof IOException)
00390         jE = new JMSException("Could not read the bytes array.");
00391       jE.setLinkedException(e);
00392       throw jE;
00393     }
00394   }
00395 
00402   public short readShort() throws JMSException
00403   {
00404     if (WObody)
00405       throw new MessageNotReadableException("Can't read the message body as"
00406                                             + " it is write-only.");
00407     try {
00408       return inputStream.readShort();
00409     }
00410     catch (Exception e) {
00411       JMSException jE = null;
00412       if (e instanceof EOFException)
00413         jE = new MessageEOFException("Unexpected end of bytes array.");
00414       else if (e instanceof IOException)
00415         jE = new JMSException("Could not read the bytes array.");
00416       jE.setLinkedException(e);
00417       throw jE;
00418     }
00419   }
00420   
00427   public int readUnsignedShort() throws JMSException
00428   {
00429     if (WObody)
00430       throw new MessageNotReadableException("Can't read the message body as"
00431                                             + " it is write-only.");
00432     try {
00433       return inputStream.readUnsignedShort();
00434     }
00435     catch (Exception e) {
00436       JMSException jE = null;
00437       if (e instanceof EOFException)
00438         jE = new MessageEOFException("Unexpected end of bytes array.");
00439       else if (e instanceof IOException)
00440         jE = new JMSException("Could not read the bytes array.");
00441       jE.setLinkedException(e);
00442       throw jE;
00443     }
00444   }
00445 
00452   public char readChar() throws JMSException
00453   {
00454     if (WObody)
00455       throw new MessageNotReadableException("Can't read the message body as"
00456                                             + " it is write-only.");
00457     try {
00458       return inputStream.readChar();
00459     }
00460     catch (Exception e) {
00461       JMSException jE = null;
00462       if (e instanceof EOFException)
00463         jE = new MessageEOFException("Unexpected end of bytes array.");
00464       else if (e instanceof IOException)
00465         jE = new JMSException("Could not read the bytes array.");
00466       jE.setLinkedException(e);
00467       throw jE;
00468     }
00469   }
00470 
00477   public int readInt() throws JMSException
00478   {
00479     if (WObody)
00480       throw new MessageNotReadableException("Can't read the message body as"
00481                                             + " it is write-only.");
00482     try {
00483       return inputStream.readInt();
00484     }
00485     catch (Exception e) {
00486       JMSException jE = null;
00487       if (e instanceof EOFException)
00488         jE = new MessageEOFException("Unexpected end of bytes array.");
00489       else if (e instanceof IOException)
00490         jE = new JMSException("Could not read the bytes array.");
00491       jE.setLinkedException(e);
00492       throw jE;
00493     }
00494   }
00495 
00502   public long readLong() throws JMSException
00503   {
00504     if (WObody)
00505       throw new MessageNotReadableException("Can't read the message body as"
00506                                             + " it is write-only.");
00507     try {
00508       return inputStream.readLong();
00509     }
00510     catch (Exception e) {
00511       JMSException jE = null;
00512       if (e instanceof EOFException)
00513         jE = new MessageEOFException("Unexpected end of bytes array.");
00514       else if (e instanceof IOException)
00515         jE = new JMSException("Could not read the bytes array.");
00516       jE.setLinkedException(e);
00517       throw jE;
00518     }
00519   }
00520 
00527 //    public float readFloat() throws JMSException
00528 //    {
00529 //      if (WObody)
00530 //        throw new MessageNotReadableException("Can't read the message body as"
00531 //                                              + " it is write-only.");
00532 //      try {
00533 //        return inputStream.readFloat();
00534 //      }
00535 //      catch (Exception e) {
00536 //        JMSException jE = null;
00537 //        if (e instanceof EOFException)
00538 //          jE = new MessageEOFException("Unexpected end of bytes array.");
00539 //        else if (e instanceof IOException)
00540 //          jE = new JMSException("Could not read the bytes array.");
00541 //        jE.setLinkedException(e);
00542 //        throw jE;
00543 //      }
00544 //    }
00545 
00552 //    public double readDouble() throws JMSException
00553 //    {
00554 //      if (WObody)
00555 //        throw new MessageNotReadableException("Can't read the message body as"
00556 //                                              + " it is write-only.");
00557 //      try {
00558 //        return inputStream.readDouble();
00559 //      }
00560 //      catch (Exception e) {
00561 //        JMSException jE = null;
00562 //        if (e instanceof EOFException)
00563 //          jE = new MessageEOFException("Unexpected end of bytes array.");
00564 //        else if (e instanceof IOException)
00565 //          jE = new JMSException("Could not read the bytes array.");
00566 //        jE.setLinkedException(e);
00567 //        throw jE;
00568 //      }
00569 //    }
00570 
00577   public int readBytes(byte[] value) throws JMSException
00578   {
00579     if (WObody)
00580       throw new MessageNotReadableException("Can't read the message body as"
00581                                             + " it is write-only.");
00582     int counter = 0;
00583 
00584     try {
00585       for (int i = 0; i < value.length; i ++) {
00586         value[i] = inputStream.readByte();
00587         counter++;
00588       }
00589     }
00590     // End of array has been reached:
00591     catch (EOFException eofE) {}
00592     // An error has occured!
00593     catch (IOException ioE) {
00594       JMSException jE = null;
00595       jE = new JMSException("Could not read the bytes array.");
00596       jE.setLinkedException(ioE);
00597       throw jE;
00598     }
00599     if (counter == 0)
00600       return -1;
00601     return counter;
00602   }
00603 
00610   public int readBytes(byte[] value, int length) throws JMSException
00611   {
00612     if (WObody)
00613       throw new MessageNotReadableException("Can't read the message body as"
00614                                             + " it is write-only.");
00615     if (length > value.length || length < 0)
00616       throw new IndexOutOfBoundsException("Invalid length parameter: "
00617                                           + length);
00618     int counter = 0;
00619 
00620     try {
00621       for (int i = 0; i < length; i ++) {
00622         value[i] = inputStream.readByte();
00623         counter++;
00624       }
00625     }
00626     // End of array has been reached:
00627     catch (EOFException eofE) {}
00628     // An error has occured!
00629     catch (IOException ioE) {
00630       JMSException jE = null;
00631       jE = new JMSException("Could not read the bytes array.");
00632       jE.setLinkedException(ioE);
00633       throw jE;
00634     }
00635     if (counter == 0)
00636       return -1;
00637     return counter;
00638   }
00639 
00646   public String readUTF() throws JMSException
00647   {
00648     if (WObody)
00649       throw new MessageNotReadableException("Can't read the message body as"
00650                                             + " it is write-only.");
00651     try {
00652       return inputStream.readUTF();
00653     }
00654     catch (Exception e) {
00655       JMSException jE = null;
00656       if (e instanceof EOFException)
00657         jE = new MessageEOFException("Unexpected end of bytes array.");
00658       else if (e instanceof IOException)
00659         jE = new JMSException("Could not read the bytes array.");
00660       jE.setLinkedException(e);
00661       throw jE;
00662     }
00663   }
00664 
00665   
00672   public void reset() throws JMSException
00673   {
00674     try {
00675       if (WObody) {
00676         outputStream.flush();
00677         bytes = outputBuffer.toByteArray();
00678       }
00679       else
00680         inputStream.close();
00681       
00682       inputStream = new DataInputStream(new ByteArrayInputStream(bytes));
00683 
00684       RObody = true;
00685       WObody = false;
00686     }
00687     catch (IOException iE) {
00688       JMSException jE =
00689         new JMSException("Error while manipulating the stream facilities.");
00690       jE.setLinkedException(iE);
00691       throw jE;
00692     }
00693   }
00694 
00701   protected void prepare() throws Exception
00702   {
00703     super.prepare();
00704 
00705     if (WObody) {
00706       outputStream.flush();
00707       bytes = outputBuffer.toByteArray();
00708       prepared = true;
00709     }
00710 
00711     momMsg.clearBody();
00712     momMsg.setBytes(bytes);
00713   } 
00714 }

Generated on Tue Sep 16 16:14:23 2008 for joram by  doxygen 1.5.0