1 /*
   2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores,
  20  * CA 94065 USA or visit www.oracle.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #ifndef _BUFFER_
  26 #define _BUFFER_
  27 
  28 // A Buffer is the backing store for the IOBuf abstraction and
  29 // supports producer-consumer filling and draining.
  30 
  31 class Buffer {
  32 public:
  33   Buffer(int bufSize);
  34   ~Buffer();
  35 
  36   char* fillPos();   // Position of the place where buffer should be filled
  37   int   remaining(); // Number of bytes that can be placed starting at fillPos
  38   int   size();      // Size of the buffer
  39   // Move up fill position by amount (decreases remaining()); returns
  40   // false if not enough space
  41   bool  incrFillPos(int amt);
  42 
  43   // Read single byte (0..255); returns -1 if no data available.
  44   int   readByte();
  45   // Read multiple bytes, non-blocking (this buffer does not define a
  46   // fill mechanism), into provided buffer. Returns number of bytes read.
  47   int   readBytes(char* buf, int len);
  48 
  49   // Access to drain position. Be very careful using this.
  50   char* drainPos();
  51   int   drainRemaining();
  52   bool  incrDrainPos(int amt);
  53 
  54   // Compact buffer, removing already-consumed input. This must be
  55   // called periodically to yield the illusion of an infinite buffer.
  56   void  compact();
  57 
  58 private:
  59   Buffer(const Buffer&);
  60   Buffer& operator=(const Buffer&);
  61 
  62   char* buf;
  63   int   sz;
  64   int   fill;
  65   int   drain;
  66 };
  67 
  68 #endif // #defined _BUFFER_