std::tuple<size_t, IOEvent> read_until_event(IInputStream& is, IOutputStream& os) { if (os.is_write_nonblocking()) { raise<IOError>("read_until_event will lose data if writing to non-blocking socket. Use a buffered approach instead."); } bool reading = true; size_t total_read = 0; size_t total_written = 0; IOEvent reason; byte buffer[1024]; while (reading) { auto r = is.read(buffer, 1024); either_switch(r, [&](size_t n) { total_written += n; either_switch(os.write(buffer, n), [&](size_t written) { total_written += written; }, [&](IOEvent wev) { reason = wev; reading = false; } ); }, [&](IOEvent event) { reason = event; reading = false; } ); } return {total_written, reason}; }
void TBmpFileGray8Base::save_copy_line(IOutputStream& aOutputStream,const void* pixelsLine)const { long BmpByteWidth=this->get_bmp_byte_width(); long pixelsWidth=this->get_width(); aOutputStream.write(pixelsLine,pixelsWidth); if (pixelsWidth<BmpByteWidth) aOutputStream.skip_fill(BmpByteWidth-pixelsWidth); }
ECode UriCodec::Decode( /* [in] */ const String& s, /* [in] */ Boolean convertPlus, /* [in] */ ICharset* charset, /* [in] */ Boolean throwOnFailure, /* [out] */ String* decodedString) { VALIDATE_NOT_NULL(decodedString); if (s.IndexOf('%') == -1 && (!convertPlus || s.IndexOf('+') == -1)) { *decodedString = s; return NOERROR; } StringBuilder result(s.GetByteLength()); AutoPtr<IByteArrayOutputStream> out; CByteArrayOutputStream::New((IByteArrayOutputStream**)&out); IOutputStream* os = IOutputStream::Probe(out); AutoPtr<ArrayOf<Char32> > char32Array = s.GetChars(); for (Int32 i = 0; i < s.GetLength();) { Char32 c = (*char32Array)[i]; if (c == '%') { do { Int32 d1, d2; if (i + 2 < s.GetLength() && (d1 = HexToInt((*char32Array)[i + 1])) != -1 && (d2 = HexToInt((*char32Array)[i + 2])) != -1) { os->Write((Byte) ((d1 << 4) + d2)); } else if (throwOnFailure) { return E_ILLEGAL_ARGUMENT_EXCEPTION; } else { // TODO: unicode escape const char* chars = "\ufffd"; AutoPtr<ArrayOf<Byte> > replacement = GetBytes(chars, charset); os->Write(replacement, 0, replacement->GetLength()); } i += 3; } while (i < s.GetLength() && (*char32Array)[i] == '%'); AutoPtr<ArrayOf<Byte> > bytes; out->ToByteArray((ArrayOf<Byte>**)&bytes); //result.append(new String(out.toByteArray(), charset); result.Append(String((char*)bytes->GetPayload())); out->Reset(); } else { if (convertPlus && c == '+') { c = ' '; } result.AppendChar(c); i++; } } *decodedString = result.ToString(); return NOERROR; }
void TBmpFileBase::save_to(IOutputStream& aOutputStream) const { long BmpByteWidth=this->get_bmp_byte_width(); WORD bfType=csBmpTag; //写入类型信息 aOutputStream.write(&bfType, sizeof(bfType)); //填充BITMAPFILEHEADER结构 BITMAPFILEHEADER_NO_TYPE bmFileHeader;//位图文件头结构和位图信息结构 memset(&bmFileHeader,0,sizeof(bmFileHeader)); DWORD dwDibBitsSize=this->get_height()*MyBase::abs(BmpByteWidth); DWORD dwOffBits =sizeof(bfType)+ sizeof(BITMAPFILEHEADER_NO_TYPE) + sizeof(BITMAPINFOHEADER) +this->get_palette_byte_size(); DWORD dwFileSize = dwOffBits - dwDibBitsSize; bmFileHeader.bfSize = dwFileSize; bmFileHeader.bfReserved1 = 0; bmFileHeader.bfReserved2 = 0; bmFileHeader.bfOffBits = dwOffBits; //写入文件头和位图信息 aOutputStream.write(&bmFileHeader, sizeof(BITMAPFILEHEADER_NO_TYPE)); // BITMAPINFOHEADER bmpInfo; memset(&bmpInfo,0,sizeof(bmpInfo)); bmpInfo.biSize= sizeof(BITMAPINFOHEADER); bmpInfo.biWidth=this->get_width(); bmpInfo.biHeight=this->get_height(); bmpInfo.biPlanes=1; bmpInfo.biBitCount=(WORD)this->get_bmp_bit_count(); bmpInfo.biCompression=0; bmpInfo.biSizeImage=0; bmpInfo.biXPelsPerMeter=0; bmpInfo.biYPelsPerMeter=0; bmpInfo.biClrUsed=0; bmpInfo.biClrImportant=0; aOutputStream.write(&bmpInfo, sizeof(BITMAPINFOHEADER)); this->write_palette(aOutputStream); //写入数据 for (long y=bmpInfo.biHeight-1; y>=0; --y) save_copy_line(aOutputStream,this->get_pixels_line(y)); }
Boolean FileUtils::CopyToFile( /* [in] */ IInputStream* inputStream, /* [in] */ IFile* destFile) { if (inputStream == NULL || destFile == NULL) { return FALSE; } Boolean result = FALSE; Boolean temp; destFile->Exists(&temp); if (temp) { destFile->Delete(&temp); } AutoPtr<IFileDescriptor> fd; AutoPtr<ArrayOf<Byte> > buffer; Int32 bytesRead; IOutputStream* os; AutoPtr<IFileOutputStream> out; ECode ec = CFileOutputStream::New(destFile, (IFileOutputStream**)&out); FAIL_GOTO(ec, _EXIT_); os = IOutputStream::Probe(out); buffer = ArrayOf<Byte>::Alloc(4096); while (inputStream->Read(buffer, &bytesRead), bytesRead >= 0) { os->Write(buffer, 0, bytesRead); } IFlushable::Probe(out)->Flush(); out->GetFD((IFileDescriptor**)&fd); fd->Sync(); result = TRUE; _EXIT_: if (out) { ICloseable::Probe(out)->Close(); } return result; }
void write(IOutputStream& out, const void* data, size_t size) { while (size > 0) { size_t writtenSize = out.writeSome(data, size); if (writtenSize == 0) { throw std::runtime_error("Failed to write to IOutputStream"); } data = static_cast<const uint8_t*>(data) + writtenSize; size -= writtenSize; } }
ECode FileUtils::ReadTextFile( /* [in] */ IFile* file, /* [in] */ Int32 max, /* [in] */ const String& ellipsis, /* [out] */ String* result) { VALIDATE_NOT_NULL(result); *result = String(""); VALIDATE_NOT_NULL(file); AutoPtr<IInputStream> input; AutoPtr<IBufferedInputStream> bis; Int64 size; Int32 length; AutoPtr<ArrayOf<Byte> > data, last, tmp; Boolean rolled = FALSE; AutoPtr<IByteArrayOutputStream> contents; IInputStream* is; IOutputStream* os; ECode ec = CFileInputStream::New(file, (IInputStream**)&input); FAIL_GOTO(ec, _EXIT_); // wrapping a BufferedInputStream around it because when reading /proc with unbuffered // input stream, bytes read not equal to buffer size is not necessarily the correct // indication for EOF; but it is true for BufferedInputStream due to its implementation. ec = CBufferedInputStream::New(input, (IBufferedInputStream**)&bis); FAIL_GOTO(ec, _EXIT_); is = IInputStream::Probe(bis); file->GetLength(&size); if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the first N bytes if (size > 0 && (max == 0 || size < max)) max = (Int32) size; data = ArrayOf<Byte>::Alloc(max + 1); is->Read(data, &length); if (length <= 0) { *result = String(""); } else if (length <= max) { *result = String((const char *)(data->GetPayload()), length); } else if (ellipsis.IsNull()) { *result = String((const char *)(data->GetPayload()), max); } else { *result = String((const char *)(data->GetPayload()), max); *result += ellipsis; } } else if (max < 0) { // "tail" mode: keep the last N do { if (last != NULL) rolled = true; tmp = last; last = data; data = tmp; if (data == NULL) data = ArrayOf<Byte>::Alloc(-max); is->Read(data, &length); } while (length == data->GetLength()); if (last == NULL && length <= 0) { *result = String(""); goto _EXIT_; } if (last == NULL) { *result = String((const char *)(data->GetPayload()), length); goto _EXIT_; } if (length > 0) { rolled = true; last->Copy(length, last, 0, last->GetLength() - length); last->Copy(last->GetLength() - length, data, 0, length); // System.arraycopy(last, length, last, 0, last.length - length); // System.arraycopy(data, 0, last, last.length - len, length); } if (ellipsis == NULL || !rolled) { *result = String((const char *)(last->GetPayload()), last->GetLength()); } else { *result = ellipsis; *result += String((const char *)(last->GetPayload()), last->GetLength()); } } else { // "cat" mode: size unknown, read it all in streaming fashion CByteArrayOutputStream::New((IByteArrayOutputStream**)&contents); os = IOutputStream::Probe(contents); data = ArrayOf<Byte>::Alloc(1024); do { is->Read(data, &length); if (length > 0) os->Write(data, 0, length); } while (length == data->GetLength()); contents->ToString(result); } _EXIT_: if (bis) ICloseable::Probe(bis)->Close(); if (input) ICloseable::Probe(input)->Close(); return ec; }
long TBmpFileGray8Base::write_palette(IOutputStream& aOutputStream) const { aOutputStream.write(gray_paltette256,256*4); return 256*4; }
void TBmpFileColor32Base::save_copy_line(IOutputStream& aOutputStream,const void* pixelsLine)const { long BmpByteWidth=this->get_bmp_byte_width(); aOutputStream.write(pixelsLine,BmpByteWidth); }
ssize_t OutputStream_write(void *cookie, const char *buf, size_t size) { IOutputStream* output = (IOutputStream*)cookie; return output->write(buf, size); }