View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.toolkit.util;
20  
21  /***
22   *
23   * @author  Mattias Hagstrand
24   * @version 1.00
25   * 
26   *//* 
27   *
28   * WHEN        WHO               WHY & WHAT
29   * -----------------------------------------------------------------------------
30   * 2002-07-26  Mattias Hagstrand Creation
31   */
32  public class CircularByteBuffer
33  {
34      // Data members ------------------------------------------------------------
35      protected byte[] mBuffer;
36      protected int mBufferSize;
37      protected int mCurrentReadPosition;
38      protected int mCurrentWritePosition;
39      protected int mAvailableSpace;
40      
41      // Constructors ------------------------------------------------------------
42      public CircularByteBuffer(int bufferSize)
43      {
44          mBuffer = new byte[bufferSize];
45          mBufferSize = bufferSize;
46          mAvailableSpace = mBufferSize;
47      }
48      
49      // Access methods ----------------------------------------------------------
50      public void addToBuffer(byte[] b, int off, int len)
51      {
52          synchronized (mBuffer)
53          {
54              // Make sure there are enough room in the buffer
55              while (this.getAvailableBufferSpace() < len)
56                  this.resizeBuffer();
57  
58              int readIndex = off;
59              int writeIndex = mCurrentWritePosition;
60              while (readIndex < off + len && writeIndex < mBuffer.length)
61              {
62                  mBuffer[writeIndex] = b[readIndex];
63                  readIndex++;
64                  writeIndex++;
65              }
66              if (readIndex < off + len)
67              {
68                  writeIndex = 0;
69                  while (readIndex < off + len)
70                  {
71                      mBuffer[writeIndex] = b[readIndex];
72                      readIndex++;
73                      writeIndex++;
74                  }
75              }
76              mCurrentWritePosition = (writeIndex < mBuffer.length ? writeIndex : 0);
77              mAvailableSpace -= len;
78          }
79      }
80  
81      public int getFromBuffer(byte[] b, int off, int len)
82      {
83          int nbrOfBytesRead = 0;
84          synchronized (mBuffer)
85          {
86              int readIndex = mCurrentReadPosition;
87              int writeIndex = off;
88              if (mCurrentReadPosition >= mCurrentWritePosition)
89              {
90                  while (readIndex < mBuffer.length && nbrOfBytesRead < len)
91                  {
92                      b[writeIndex] = mBuffer[readIndex];
93                      readIndex++;
94                      writeIndex++;
95                      nbrOfBytesRead++;
96                  }
97                  if (nbrOfBytesRead < len)
98                      readIndex = 0;
99              }
100             while (readIndex < mCurrentWritePosition &&
101                    nbrOfBytesRead < len)
102             {
103                 b[writeIndex] = mBuffer[readIndex];
104                 readIndex++;
105                 writeIndex++;
106                 nbrOfBytesRead++;
107             }
108             mCurrentReadPosition = (readIndex < mBuffer.length ? readIndex : 0);
109             mAvailableSpace += nbrOfBytesRead;
110         }
111         return nbrOfBytesRead;
112     }
113 
114     public int getBufferSize()
115     {
116         synchronized (mBuffer)
117         {
118             return (mBuffer.length - mAvailableSpace);
119         }
120     }
121 
122     // Help methods ------------------------------------------------------------
123     protected void resizeBuffer()
124     {
125         synchronized (mBuffer)
126         {
127             byte[] newBuffer = new byte[mBuffer.length + mBufferSize / 2];
128             int bufferSize = this.getBufferSize();
129             this.getFromBuffer(newBuffer, 0, bufferSize);
130             mCurrentReadPosition = 0;
131             mCurrentWritePosition = bufferSize;
132             mBuffer = newBuffer;
133             mAvailableSpace = mBuffer.length - bufferSize;
134         }
135     }
136 
137     protected int getAvailableBufferSpace()
138     {
139         synchronized (mBuffer)
140         {
141             return mAvailableSpace;
142         }
143     }
144 }