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 #include "Buffer.hpp" 26 27 #include <string.h> 28 29 Buffer::Buffer(int bufSize) { 30 buf = new char[bufSize]; 31 sz = bufSize; 32 fill = 0; 33 drain = 0; 34 } 35 36 Buffer::~Buffer() { 37 delete[] buf; 38 } 39 40 char* 41 Buffer::fillPos() { 42 return buf + fill; 43 } 44 45 int 46 Buffer::remaining() { 47 return sz - fill; 48 } 49 50 int 51 Buffer::size() { 52 return sz; 53 } 54 55 bool 56 Buffer::incrFillPos(int amt) { 57 if (fill + amt >= sz) { 58 return false; 59 } 60 fill += amt; 61 return true; 62 } 63 64 int 65 Buffer::readByte() { 66 if (drain < fill) { 67 return buf[drain++] & 0xFF; 68 } else { 69 return -1; 70 } 71 } 72 73 int 74 Buffer::readBytes(char* data, int len) { 75 int numRead = 0; 76 while (numRead < len) { 77 int c = readByte(); 78 if (c < 0) break; 79 data[numRead++] = (char) c; 80 } 81 return numRead; 82 } 83 84 char* 85 Buffer::drainPos() { 86 return buf + drain; 87 } 88 89 int 90 Buffer::drainRemaining() { 91 return fill - drain; 92 } 93 94 bool 95 Buffer::incrDrainPos(int amt) { 96 if (drainRemaining() < amt) { 97 return false; 98 } 99 drain += amt; 100 return true; 101 } 102 103 void 104 Buffer::compact() { 105 // Copy down data 106 memmove(buf, buf + drain, fill - drain); 107 // Adjust positions 108 fill -= drain; 109 drain = 0; 110 }