4 Replies Latest reply on Jan 25, 2010 4:43 AM by Jeff Mesnil

    Reading and Writing Lines to Buffer

    Tim Fox Master

      I had a quick look at the Stomp implementation.

       

      Looks good, but I think some of the code that reads and writes lines to the buffers is pretty clunky and inefficient.

       

      Instead of using streams you can do something like this.

       

         private void writeLine(final HornetQBuffer buffer, final String line)
         {
            for (int i = 0; i < line.length(); i++)
            {
               buffer.writeByte((byte)line.charAt(i));
            }
            buffer.writeByte((byte)'\n');
         }
        
         public void testFoo() throws Exception
         {
            HornetQBuffer buffer = HornetQBuffers.dynamicBuffer(1000);
           
            writeLine(buffer, "This is line 1");
            writeLine(buffer, "This is line 2");
            writeLine(buffer, "This is line 3");
            writeLine(buffer, "This is line 4");

            //How to read ascii from a byte buffer
           
            char[] chars = new char[1000];
           
            int count = 0;
            while (buffer.readable())
            {
               byte b = buffer.readByte();
              
               if (b == (byte)'\n')
               {
                  String s = new String(chars, 0, count);
                 
                  log.info("Line is:" + s);
                 
                  count = 0;
               }
               else
               {
                  chars[count++] = (char)b;
               }
            }
           
           
         }