1 /*
2  * BlockBufferedFileBinaryOutputPort.h - <file binary output port>
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
5  *   Copyright (c) 2009  Kokosabu(MIURA Yasuyuki)  <kokosabu@gmail.com>
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *   1. Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *
14  *   2. Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *  $Id$
31  */
32 
33 #ifndef SCHEME_BLOCK_BUFFERED_FILE_BINARY_OUTPUT_PORT_
34 #define SCHEME_BLOCK_BUFFERED_FILE_BINARY_OUTPUT_PORT_
35 
36 #include "BufferedFileBinaryOutputPort.h"
37 
38 namespace scheme {
39 
40 class BlockBufferedFileBinaryOutputPort : public BufferedFileBinaryOutputPort
41 {
42 public:
BlockBufferedFileBinaryOutputPort(File * file)43     BlockBufferedFileBinaryOutputPort(File* file) : BufferedFileBinaryOutputPort(file) {}
BlockBufferedFileBinaryOutputPort(ucs4string file)44     BlockBufferedFileBinaryOutputPort(ucs4string file) : BufferedFileBinaryOutputPort(file) {}
BlockBufferedFileBinaryOutputPort(ucs4string file,int openFlags)45     BlockBufferedFileBinaryOutputPort(ucs4string file, int openFlags) : BufferedFileBinaryOutputPort(file, openFlags) {}
~BlockBufferedFileBinaryOutputPort()46     virtual ~BlockBufferedFileBinaryOutputPort() {}
47 
bufferMode()48     virtual enum bufferMode bufferMode() const
49     {
50         return BLOCK;
51     }
52 
53 protected:
writeToBuffer(uint8_t * data,int64_t reqSize)54     int64_t writeToBuffer(uint8_t* data, int64_t reqSize)
55     {
56         int64_t writeSize = 0;
57         while (writeSize < reqSize) {
58             MOSH_ASSERT(BUF_SIZE >= bufIdx_);
59             const int64_t bufDiff = BUF_SIZE - bufIdx_;
60             MOSH_ASSERT(reqSize > writeSize);
61             const int64_t sizeDiff = reqSize - writeSize;
62             if (bufDiff >= sizeDiff) {
63                 moshMemcpy(buffer_+bufIdx_, data+writeSize, sizeDiff);
64                 bufIdx_ += sizeDiff;
65                 writeSize += sizeDiff;
66             } else {
67                 moshMemcpy(buffer_+bufIdx_, data+writeSize, bufDiff);
68                 bufIdx_ += bufDiff;
69                 writeSize += bufDiff;
70                 flush();
71             }
72         }
73         return writeSize;
74     }
75 };
76 
77 } // namespace scheme
78 
79 #endif // SCHEME_LINE_BUFFERED_FILE_BINARY_OUTPUT_PORT_
80