filestream.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "filestream.hh"
  19. using namespace hadoop;
  20. hadoop::FileInStream::FileInStream()
  21. {
  22. mFile = NULL;
  23. }
  24. bool hadoop::FileInStream::open(const std::string& name)
  25. {
  26. mFile = fopen(name.c_str(), "rb");
  27. return (mFile != NULL);
  28. }
  29. ssize_t hadoop::FileInStream::read(void *buf, size_t len)
  30. {
  31. return fread(buf, 1, len, mFile);
  32. }
  33. bool hadoop::FileInStream::skip(size_t nbytes)
  34. {
  35. return (0==fseek(mFile, nbytes, SEEK_CUR));
  36. }
  37. bool hadoop::FileInStream::close()
  38. {
  39. int ret = fclose(mFile);
  40. mFile = NULL;
  41. return (ret==0);
  42. }
  43. hadoop::FileInStream::~FileInStream()
  44. {
  45. if (mFile != NULL) {
  46. close();
  47. }
  48. }
  49. hadoop::FileOutStream::FileOutStream()
  50. {
  51. mFile = NULL;
  52. }
  53. bool hadoop::FileOutStream::open(const std::string& name, bool overwrite)
  54. {
  55. if (!overwrite) {
  56. mFile = fopen(name.c_str(), "rb");
  57. if (mFile != NULL) {
  58. fclose(mFile);
  59. return false;
  60. }
  61. }
  62. mFile = fopen(name.c_str(), "wb");
  63. return (mFile != NULL);
  64. }
  65. ssize_t hadoop::FileOutStream::write(const void* buf, size_t len)
  66. {
  67. return fwrite(buf, 1, len, mFile);
  68. }
  69. bool hadoop::FileOutStream::advance(size_t nbytes)
  70. {
  71. return (0==fseek(mFile, nbytes, SEEK_CUR));
  72. }
  73. bool hadoop::FileOutStream::close()
  74. {
  75. int ret = fclose(mFile);
  76. mFile = NULL;
  77. return (ret == 0);
  78. }
  79. hadoop::FileOutStream::~FileOutStream()
  80. {
  81. if (mFile != NULL) {
  82. close();
  83. }
  84. }