// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: FileOutputStream.jlc,v 1.9 2006-01-12 21:13:07 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/FileOutputStream.h" #include "jakelib2/io/File.h" #include "jakelib2/lang/System.h" #ifdef HAVE_ERRNO_H # include #endif #ifdef HAVE_FCNTL_H # include #endif #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_SYS_STAT_H # include #endif using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.io.FileOutputStream", FileOutputStream, OutputStream) /*****************************************************************************\ * FileOutputStream | *****************************************************************************/ FileOutputStream::FileOutputStream(File* file, jboolean append) { this->append = append; init(file->getPath()); } FileOutputStream::FileOutputStream(char *filename, jboolean append) { this->append = append; String _filename(filename); init(&_filename); } FileOutputStream::FileOutputStream(String* filename, jboolean append) { this->append = append; init(filename); } FileOutputStream::~FileOutputStream() { close(); } /*****************************************************************************\ * init | *****************************************************************************/ void FileOutputStream::init(String* filename) { #if defined(JAKELIB_WIN32API) // FIXME: append handle = CreateFile(JAKELIB_LATIN1(filename), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { throw new IOException(System::explainErrorCode(GetLastError()) .. JAKELIB_AT2("jakelib.io.FileOutputStream.init")); } #else handle = open(JAKELIB_LATIN1(filename), O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC), S_IRUSR | S_IWUSR | S_IRGRP); if (handle == -1) { throw new IOException(System::explainErrorCode(errno) .. JAKELIB_AT2("jakelib.io.FileOutputStream.init")); } #endif } /*****************************************************************************\ * write | *****************************************************************************/ void FileOutputStream::write(int b) { jbyte c = (jbyte) b; write(&c, 0, 1); } /*****************************************************************************\ * write | *****************************************************************************/ void FileOutputStream::write(jbyte* b, int offset, int len) { if (b == null) throw new NullPointerException(); else if (len < 0) throw new IndexOutOfBoundsException(); else if (len == 0) return; #if defined(JAKELIB_WIN32API) DWORD written; if (!WriteFile(handle, &b[offset], len, &written, NULL)) { throw new IOException(System::explainErrorCode(GetLastError()) .. JAKELIB_AT2("jakelib.io.FileOutputStream.write")); } #else if (::write(handle, &b[offset], len) == -1) { throw new IOException(System::explainErrorCode(errno) .. JAKELIB_AT2("jakelib::io::PipeOutputStream.write")); } #endif } /*****************************************************************************\ * close | *****************************************************************************/ void FileOutputStream::close() { #if defined(JAKELIB_WIN32API) if (handle != INVALID_HANDLE_VALUE) { CloseHandle(handle); handle = INVALID_HANDLE_VALUE; } #else if (handle != -1) { ::close(handle); handle = -1; } #endif }