private void loadLogPosition() { if (m_pPosFile == null) { m_pPosFile = new CRhoFile(); } if (!m_pPosFile.isOpened()) { String strPosPath = getLogConf().getLogFilePath() + "_pos"; m_pPosFile.open(strPosPath, CRhoFile.EOpenModes.OpenForReadWrite); } if (!m_pPosFile.isOpened()) { return; } m_pPosFile.movePosToStart(); String strPos = m_pPosFile.readString(); if (strPos.length() == 0) { return; } m_nCirclePos = int.Parse(strPos); if (m_nCirclePos < 0 || m_nCirclePos > (int)m_nFileLogSize) { m_nCirclePos = -1; } if (m_nCirclePos >= 0) { m_pFile.setPosTo(m_nCirclePos); } }
public void writeLogMessage(String strMsg) { try{ int len = strMsg.length(); if (m_pFile == null) { m_pFile = new CRhoFile(); } if (!m_pFile.isOpened()) { m_pFile.open(getLogConf().getLogFilePath(), CRhoFile.EOpenModes.OpenForAppend); m_nFileLogSize = (int)m_pFile.size(); loadLogPosition(); } if (getLogConf().getMaxLogFileSize() > 0) { if ((m_nCirclePos >= 0 && m_nCirclePos + len > getLogConf().getMaxLogFileSize()) || (m_nCirclePos < 0 && m_nFileLogSize + len > getLogConf().getMaxLogFileSize())) { m_pFile.movePosToStart(); m_nFileLogSize = 0; m_nCirclePos = 0; } } //int nWritten = m_pFile.writeString(strMsg); m_pFile.writeString(strMsg); m_pFile.flush(); if (m_nCirclePos >= 0) { m_nCirclePos += len; } else { m_nFileLogSize += len; } saveLogPosition(); }catch (Exception exc) { log(exc.Message); } }
void processMultipartItems(Vector <MultipartItem> arItems) { for (int i = 0; i < (int)arItems.size(); i++) { MultipartItem oItem = (MultipartItem)arItems.elementAt(i); if (oItem.m_strName.length() == 0) { oItem.m_strName = "blob"; } if (oItem.m_strFileName.length() == 0) { if (oItem.m_strFilePath.length() > 0) { oItem.m_strFileName = CFilePath.getBaseName(oItem.m_strFilePath); } //else // oItem.m_strFileName = "doesnotmatter.txt"; } oItem.m_strDataPrefix = i > 0 ? "\r\n" : ""; oItem.m_strDataPrefix += "------------A6174410D6AD474183FDE48F5662FCC5\r\n" + "Content-Disposition: form-data; name=\""; oItem.m_strDataPrefix += oItem.m_strName + "\""; if (oItem.m_strFileName.length() > 0) { oItem.m_strDataPrefix += "; filename=\"" + oItem.m_strFileName + "\""; } oItem.m_strDataPrefix += "\r\n"; if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0) { oItem.m_strDataPrefix += "Content-Type: " + oItem.m_strContentType + "\r\n"; } long nContentSize = 0; if (oItem.m_strFilePath.length() > 0) { CRhoFile file = null; try{ file = RhoClassFactory.createFile(); file.open(oItem.m_strFilePath, CRhoFile.EOpenModes.OpenReadOnly); nContentSize = file.size(); if (!file.isOpened()) { LOG.ERROR("File not found: " + oItem.m_strFilePath); throw new Exception("File not found:" + oItem.m_strFilePath); } }finally{ if (file != null) { try{ file.close(); }catch (IOException e) { LOG.ERROR("file closing failed.", e); } } } } else { nContentSize = oItem.m_strBody.length(); } if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0) { oItem.m_strDataPrefix += "Content-Length: " + nContentSize + "\r\n"; } oItem.m_strDataPrefix += "\r\n"; } }
private void GetRequestStreamCallback(IAsyncResult asyncResult) { Stream stream = null; try { stream = m_webRequest.EndGetRequestStream(asyncResult); if (m_strBody != null) { stream.Write(new UTF8Encoding().GetBytes(m_strBody), 0, m_strBody.length());//TODO ASCII ??? } else if (m_isMultiPart) { for (int i = 0; i < (int)m_arItems.size(); i++) { MultipartItem oItem = (MultipartItem)m_arItems.elementAt(i); stream.Write(new UTF8Encoding().GetBytes(oItem.m_strDataPrefix), 0, oItem.m_strDataPrefix.length()); if (oItem.m_strFilePath.length() > 0) { IInputStream fis = null; CRhoFile file = RhoClassFactory.createFile(); try { file.open(oItem.m_strFilePath, CRhoFile.EOpenModes.OpenReadOnly); if (!file.isOpened()) { LOG.ERROR("File not found: " + oItem.m_strFilePath); throw new Exception("File not found:" + oItem.m_strFilePath); } fis = file.getInputStream(); byte[] byteBuffer = new byte[1024 * 4]; int nRead = 0; do { nRead = fis.read(byteBuffer, 0, 1024 * 4); if (nRead > 0) { stream.Write(byteBuffer, 0, nRead); } } while (nRead > 0); } finally { if (file != null) { try { file.close(); } catch (IOException e) { LOG.ERROR("GetRequestStreamCallback: file close failed.", e); } } } } else { stream.Write(new UTF8Encoding().GetBytes(oItem.m_strBody), 0, oItem.m_strBody.length()); } } stream.Write(new UTF8Encoding().GetBytes(szMultipartPostfix), 0, szMultipartPostfix.length()); } LOG.INFO("write body done"); } finally { if (stream != null) { stream.Close(); } m_reqWaitEvent.Set(); } }