1 /*
2  * ByteArrayBinaryInputPort.cpp -
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  $Id: ByteArrayBinaryInputPort.cpp 183 2008-07-04 06:19:28Z higepon $
30  */
31 
32 #include "Object.h"
33 #include "Object-inl.h"
34 #include "Pair.h"
35 #include "Pair-inl.h"
36 #include "ByteVector.h"
37 #include "ByteArrayBinaryInputPort.h"
38 #include "Fixnum.h"
39 #include "Bignum.h"
40 
41 
42 using namespace scheme;
43 
ByteArrayBinaryInputPort(const uint8_t * buf,int64_t size)44 ByteArrayBinaryInputPort::ByteArrayBinaryInputPort(const uint8_t* buf, int64_t size) : buf_(buf), size_(size), index_(0), isClosed_(false), isPseudoClosed_(false)
45 {
46 }
47 
~ByteArrayBinaryInputPort()48 ByteArrayBinaryInputPort::~ByteArrayBinaryInputPort()
49 {
50     close();
51 }
52 
toString()53 ucs4string ByteArrayBinaryInputPort::toString() {
54     return UC("<byte-array-input-port>");
55 }
56 
readBytes(uint8_t * buf,int64_t reqSize,bool & isErrorOccured)57 int64_t ByteArrayBinaryInputPort::readBytes(uint8_t* buf, int64_t reqSize, bool& isErrorOccured)
58 {
59     const int64_t restSize = size_ - index_;
60     const int64_t sizeToRead = (reqSize > restSize) ? restSize : reqSize;
61     moshMemcpy(buf, &(buf_[index_]), sizeToRead);
62     index_ += sizeToRead;
63     return sizeToRead;
64 }
65 
readSome(uint8_t ** buf,bool & isErrorOccured)66 int64_t ByteArrayBinaryInputPort::readSome(uint8_t** buf, bool& isErrorOccured)
67 {
68     return readAll(buf, isErrorOccured);
69 }
70 
readAll(uint8_t ** buf,bool & isErrorOccured)71 int64_t ByteArrayBinaryInputPort::readAll(uint8_t** buf, bool& isErrorOccured)
72 {
73     const int64_t restSize = size_ - index_;
74     uint8_t* dest = allocatePointerFreeU8Array(restSize);
75     moshMemcpy(dest, &buf_[index_], restSize);
76     *buf = dest;
77     return restSize;
78 }
79 
open()80 int ByteArrayBinaryInputPort::open()
81 {
82     return MOSH_SUCCESS;
83 }
84 
isClosed() const85 bool ByteArrayBinaryInputPort::isClosed() const
86 {
87     return isClosed_ || isPseudoClosed_;
88 }
89 
close()90 int ByteArrayBinaryInputPort::close()
91 {
92     isClosed_ = true;
93     return MOSH_SUCCESS;
94 }
95 
pseudoClose()96 int ByteArrayBinaryInputPort::pseudoClose()
97 {
98     isPseudoClosed_ = true;
99     return MOSH_SUCCESS;
100 }
101 
position() const102 Object ByteArrayBinaryInputPort::position() const
103 {
104     return Bignum::makeIntegerFromS64(index_);
105 }
106 
getFile()107 File* ByteArrayBinaryInputPort::getFile()
108 {
109     return NULL;
110 }
111