void ActiveMQTextMessage::beforeMarshal( wireformat::WireFormat* wireFormat ) throw ( decaf::io::IOException ) { ActiveMQMessageTemplate<cms::TextMessage>::beforeMarshal( wireFormat ); if( this->text.get() != NULL ) { ByteArrayOutputStream* bytesOut = new ByteArrayOutputStream; OutputStream* os = bytesOut; if( this->connection != NULL && this->connection->isUseCompression() ) { this->compressed = true; os = new DeflaterOutputStream( os, true ); } DataOutputStream dataOut( os, true ); if( this->text.get() == NULL ) { dataOut.writeInt( -1 ); } else { MarshallingSupport::writeString32( dataOut, *( this->text ) ); } dataOut.close(); if( bytesOut->size() > 0 ) { std::pair<const unsigned char*, int> array = bytesOut->toByteArray(); this->setContent( std::vector<unsigned char>( array.first, array.first + array.second ) ); delete [] array.first; } this->text.reset( NULL ); } }
void ActiveMQObjectMessageMarshallerTest::testTightMarshal() { ActiveMQObjectMessageMarshaller marshaller; Properties props; OpenWireFormat openWireFormat( props ); // Configure for this test. openWireFormat.setVersion( 5 ); openWireFormat.setTightEncodingEnabled( true ); ActiveMQObjectMessage outCommand; ActiveMQObjectMessage inCommand; Pointer<ProducerId> producerId( new ProducerId() ); producerId->setConnectionId( "ConnectionId" ); producerId->setSessionId( 123 ); producerId->setValue( 42 ); Pointer<MessageId> messageId( new MessageId() ); messageId->setBrokerSequenceId( 1 ); messageId->setProducerSequenceId( 3 ); messageId->setProducerId( producerId ); outCommand.setMessageId( messageId ); try { // Marshal the dataStructure to a byte array. ByteArrayOutputStream baos; DataOutputStream dataOut( &baos ); // Phase 1 - count the size int size = 1; BooleanStream bs; size += marshaller.tightMarshal1( &openWireFormat, &outCommand, &bs ); size += bs.marshalledSize(); // Phase 2 - marshal dataOut.writeByte( outCommand.getDataStructureType() ); bs.marshal( &dataOut ); marshaller.tightMarshal2( &openWireFormat, &outCommand, &dataOut, &bs ); // Now read it back in and make sure it's all right. std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second, true ); DataInputStream dataIn( &bais ); unsigned char dataType = dataIn.readByte(); CPPUNIT_ASSERT( dataType == outCommand.getDataStructureType() ); bs.clear(); bs.unmarshal( &dataIn ); marshaller.tightUnmarshal( &openWireFormat, &inCommand, &dataIn, &bs ); CPPUNIT_ASSERT( inCommand.equals( &outCommand ) == true ); } catch( ActiveMQException& e ) { e.printStackTrace(); CPPUNIT_ASSERT( false ); } catch( ... ) { CPPUNIT_ASSERT( false ); } }
void ActiveMQObjectMessage::setObjectBytes(const std::vector<unsigned char>& bytes) { this->failIfReadOnlyBody(); try { if (bytes.size() == 0) { return; } if (this->connection != NULL && this->connection->isUseCompression()) { this->compressed = true; ByteArrayOutputStream bytesOut; Deflater* deflator = new Deflater(this->connection->getCompressionLevel()); DeflaterOutputStream out(&bytesOut, deflator, false, true); out.write(&bytes[0], (int)bytes.size()); std::pair<unsigned char*, int> array = bytesOut.toByteArray(); this->setContent(std::vector<unsigned char>(array.first, array.first + array.second)); delete[] array.first; } else { this->setContent(bytes); } } AMQ_CATCH_ALL_THROW_CMSEXCEPTION() }
void BaseDataStreamMarshallerTest::testTightMarshal() { SimpleDataStructureMarshaller* simpleMarshaller = new SimpleDataStructureMarshaller(); ComplexDataStructureMarshaller* complexMarshaller = new ComplexDataStructureMarshaller(); Properties props; OpenWireFormat openWireFormat(props); openWireFormat.addMarshaller( simpleMarshaller ); openWireFormat.addMarshaller( complexMarshaller ); // Marshal the dataStructure to a byte array. ByteArrayOutputStream baos; DataOutputStream dataOut( &baos ); // Phase 1 - count the size int size = 1; BooleanStream bs; size += complexMarshaller->tightMarshal1( &openWireFormat, dataStructure, &bs ); size += bs.marshalledSize(); // Phase 2 - marshal dataOut.writeByte( dataStructure->getDataStructureType() ); bs.marshal( &dataOut ); complexMarshaller->tightMarshal2( &openWireFormat, dataStructure, &dataOut, &bs ); // Now read it back in and make sure it's all right. std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second ); DataInputStream dataIn( &bais ); unsigned char dataType = dataIn.readByte(); CPPUNIT_ASSERT( dataType == dataStructure->getDataStructureType() ); ComplexDataStructure ds; bs.clear(); bs.unmarshal( &dataIn ); complexMarshaller->tightUnmarshal( &openWireFormat, &ds, &dataIn, &bs ); CPPUNIT_ASSERT_EQUAL( dataStructure->boolValue, ds.boolValue ); CPPUNIT_ASSERT( ds.cachedChild != NULL ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->boolValue, ds.cachedChild->boolValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->charValue, ds.cachedChild->charValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->shortValue, ds.cachedChild->shortValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->intValue, ds.cachedChild->intValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->longValue1, ds.cachedChild->longValue1 ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->longValue2, ds.cachedChild->longValue2 ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->longValue3, ds.cachedChild->longValue3 ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->longValue4, ds.cachedChild->longValue4 ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->longValue5, ds.cachedChild->longValue5 ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->floatValue, ds.cachedChild->floatValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->doubleValue, ds.cachedChild->doubleValue ); CPPUNIT_ASSERT_EQUAL( dataStructure->cachedChild->stringValue, ds.cachedChild->stringValue ); delete [] array.first; }
void FilterOutputStreamTest::testFlush() { try { ByteArrayOutputStream baos; FilterOutputStream os( &baos ); os.write( (unsigned char*)&testString[0], (int)testString.size(), 0, 500 ); os.flush(); CPPUNIT_ASSERT_MESSAGE( "Bytes not written after flush", 500 == baos.size() ); os.close(); } catch( IOException& e ) { CPPUNIT_FAIL("Flush test failed : " + e.getMessage()); } }
void MessageIdMarshallerTest::testTightMarshal() { MessageIdMarshaller marshaller; Properties props; OpenWireFormat openWireFormat( props ); // Configure for this test. openWireFormat.setVersion( 9 ); openWireFormat.setTightEncodingEnabled( true ); MessageId outCommand; MessageId inCommand; try { // Marshal the dataStructure to a byte array. ByteArrayOutputStream baos; DataOutputStream dataOut( &baos ); // Phase 1 - count the size int size = 1; BooleanStream bs; size += marshaller.tightMarshal1( &openWireFormat, &outCommand, &bs ); size += bs.marshalledSize(); // Phase 2 - marshal dataOut.writeByte( outCommand.getDataStructureType() ); bs.marshal( &dataOut ); marshaller.tightMarshal2( &openWireFormat, &outCommand, &dataOut, &bs ); // Now read it back in and make sure it's all right. std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second, true ); DataInputStream dataIn( &bais ); unsigned char dataType = dataIn.readByte(); CPPUNIT_ASSERT( dataType == outCommand.getDataStructureType() ); bs.clear(); bs.unmarshal( &dataIn ); marshaller.tightUnmarshal( &openWireFormat, &inCommand, &dataIn, &bs ); CPPUNIT_ASSERT( inCommand.equals( (DataStructure*) &outCommand ) == true ); } catch( ActiveMQException& e ) { e.printStackTrace(); CPPUNIT_ASSERT( false ); } catch( ... ) { CPPUNIT_ASSERT( false ); } }
void FilterOutputStreamTest::testWrite2() { try { ByteArrayOutputStream baos; FilterOutputStream os( &baos ); os.write('t'); std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second, true ); os.flush(); CPPUNIT_ASSERT_MESSAGE( "Byte not written after flush", 1 == bais.available() ); unsigned char wbytes[1]; bais.read( wbytes, 1, 0, 1 ); CPPUNIT_ASSERT_MESSAGE("Incorrect byte written", 't' == wbytes[0] ); } catch( IOException& e ) { CPPUNIT_FAIL("Write test failed : " + e.getMessage()); } }
void FilterOutputStreamTest::testWrite1() { try { ByteArrayOutputStream baos; FilterOutputStream os( &baos ); os.write( (unsigned char*)&testString[0], (int)testString.size(), 0, (int)testString.size() ); std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second, true ); os.flush(); CPPUNIT_ASSERT_MESSAGE( "Bytes not written after flush", bais.available() == (int)testString.length() ); unsigned char* wbytes = new unsigned char[ testString.length() ]; bais.read( wbytes, (int)testString.length(), 0, (int)testString.length() ); CPPUNIT_ASSERT_MESSAGE("Incorrect bytes written", testString == string( (const char*)wbytes, testString.length() ) ); delete [] wbytes; } catch( IOException& e ) { CPPUNIT_FAIL("Write test failed : " + e.getMessage()); } }
void DataOutputStreamTest::testWriteUTFStringLength() { // String of length 65536 of Null Characters. // Expect: UTFDataFormatException. std::string testStr( 65536, char('a') ); CPPUNIT_ASSERT_THROW_MESSAGE( "Should throw a UTFDataFormatException", os->writeUTF( testStr ), UTFDataFormatException ); baos->reset(); // String of length 65535 of non Null Characters since Null encodes as UTF-8. // Expect: Success. testStr.resize( 65535 ); CPPUNIT_ASSERT_NO_THROW_MESSAGE( "String of 65535 Non-Null chars should not throw.", os->writeUTF( testStr ) ); baos->reset(); // Set one of the 65535 bytes to a value that will result in a 2 byte UTF8 encoded sequence. // This will cause the string of length 65535 to have a utf length of 65536. // Expect: UTFDataFormatException. testStr[0] = char( 255 ); CPPUNIT_ASSERT_THROW_MESSAGE( "Should throw an UTFDataFormatException", os->writeUTF( testStr ), UTFDataFormatException ); // Test that a zero length string write the zero size marker. ByteArrayOutputStream byteOut; DataOutputStream dataOut( &byteOut ); dataOut.writeUTF( "" ); CPPUNIT_ASSERT( dataOut.size() == 2 ); std::pair<const unsigned char*, int> array = byteOut.toByteArray(); ByteArrayInputStream byteIn( array.first, array.second, true ); DataInputStream dataIn( &byteIn ); CPPUNIT_ASSERT( dataIn.readUnsignedShort() == 0 ); }
void ConnectionIdMarshallerTest::testLooseMarshal() { ConnectionIdMarshaller marshaller; Properties props; OpenWireFormat openWireFormat( props ); // Configure for this test. openWireFormat.setVersion( 6 ); openWireFormat.setTightEncodingEnabled( false ); ConnectionId outCommand; ConnectionId inCommand; try { // Marshal the dataStructure to a byte array. ByteArrayOutputStream baos; DataOutputStream dataOut( &baos ); dataOut.writeByte( outCommand.getDataStructureType() ); marshaller.looseMarshal( &openWireFormat, &outCommand, &dataOut ); // Now read it back in and make sure it's all right. std::pair<const unsigned char*, int> array = baos.toByteArray(); ByteArrayInputStream bais( array.first, array.second, true ); DataInputStream dataIn( &bais ); unsigned char dataType = dataIn.readByte(); CPPUNIT_ASSERT( dataType == outCommand.getDataStructureType() ); marshaller.looseUnmarshal( &openWireFormat, &inCommand, &dataIn ); CPPUNIT_ASSERT( inCommand.equals( &outCommand ) == true ); } catch( ActiveMQException& e ) { e.printStackTrace(); CPPUNIT_ASSERT( false ); } catch( ... ) { CPPUNIT_ASSERT( false ); } }
void ByteArrayOutputStreamBenchmark::run(){ int numRuns = 100; ByteArrayOutputStream bos; for( int iy = 0; iy < numRuns; ++iy ){ bos.write( (char)65 ); } bos.reset(); for( int iy = 0; iy < numRuns; ++iy ){ bos.write( buffer, bufferSize, 0, bufferSize ); } bos.reset(); for( int iy = 0; iy < numRuns; ++iy ){ bos.write( &stlBuffer[0], bufferSize ); } bos.reset(); }
void DataOutputStreamTest::test(){ unsigned char byteVal = (unsigned char)'T'; unsigned short shortVal = 5; unsigned int intVal = 10000; unsigned long long longVal = 1000000000; float floatVal = 10.0f; double doubleVal = 100.0; unsigned char arrayVal[3] = { 'a', 'b', 'c' }; // Create the stream with the buffer we just wrote to. ByteArrayOutputStream myStream; DataOutputStream writer( &myStream ); writer.writeByte( byteVal ); writer.writeShort( shortVal ); writer.writeInt( intVal ); writer.writeLong( longVal ); writer.writeFloat( floatVal ); writer.writeDouble( doubleVal ); writer.write( arrayVal, 3, 0, 3 ); std::pair<const unsigned char*, int> buffer = myStream.toByteArray(); int ix = 0; unsigned char tempByte = buffer.first[ix]; CPPUNIT_ASSERT( tempByte == byteVal ); ix += (int)sizeof( tempByte ); unsigned short tempShort = 0; memcpy( &tempShort, buffer.first+ix, sizeof( unsigned short ) ); tempShort = util::Endian::byteSwap( tempShort ); CPPUNIT_ASSERT( tempShort == shortVal ); ix += (int)sizeof( tempShort ); unsigned int tempInt = 0; memcpy( &tempInt, buffer.first+ix, sizeof( unsigned int ) ); tempInt = util::Endian::byteSwap( tempInt ); CPPUNIT_ASSERT( tempInt == intVal ); ix += (int)sizeof( tempInt ); unsigned long long tempLong = 0; memcpy( &tempLong, buffer.first+ix, sizeof( unsigned long long ) ); tempLong = util::Endian::byteSwap( tempLong ); CPPUNIT_ASSERT( tempLong == longVal ); ix += (int)sizeof( tempLong ); float tempFloat = 0; memcpy( &tempFloat, buffer.first+ix, sizeof( float ) ); tempFloat = util::Endian::byteSwap( tempFloat ); CPPUNIT_ASSERT( tempFloat == floatVal ); ix += (int)sizeof( tempFloat ); double tempDouble = 0; memcpy( &tempDouble, buffer.first+ix, sizeof( double ) ); tempDouble = util::Endian::byteSwap( tempDouble ); CPPUNIT_ASSERT( tempDouble == doubleVal ); ix += (int)sizeof( tempDouble ); delete [] buffer.first; }
void FileTransferRequestHandler::fileListRequested() { UINT8 requestedCompressionLevel; WinFilePath fullPathName; // // Read input data // { requestedCompressionLevel = m_input->readUInt8(); m_input->readUTF8(&fullPathName); } m_log->message(_T("File list of folder '%s' requested"), fullPathName.getString()); checkAccess(); UINT8 compressionLevel = requestedCompressionLevel; UINT32 compressedSize = 0; UINT32 uncompressedSize = 0; UINT32 filesCount = 0; UINT32 filesInfoDataSize = 0; const FileInfo *files = NULL; // // Get file list from specified folder // FolderListener folderListener(fullPathName.getString()); if (!folderListener.list()) { throw SystemException(); } files = folderListener.getFilesInfo(); filesCount = folderListener.getFilesCount(); // // Create buffer with "CompressedData" block inside // ByteArrayOutputStream memStream; DataOutputStream outMemStream(&memStream); outMemStream.writeUInt32(filesCount); for (UINT32 i = 0; i < filesCount; i++) { outMemStream.writeUInt64(files[i].getSize()); outMemStream.writeUInt64(files[i].lastModified()); outMemStream.writeUInt16(files[i].getFlags()); outMemStream.writeUTF8(files[i].getFileName()); } // for _ASSERT((UINT32)memStream.size() == memStream.size()); uncompressedSize = (UINT32)memStream.size(); // // Buffer for data in "CompressedData" block // compressedSize = uncompressedSize; if (compressionLevel != 0) { m_deflater.setInput(memStream.toByteArray(), memStream.size()); m_deflater.deflate(); _ASSERT((UINT32)m_deflater.getOutputSize() == m_deflater.getOutputSize()); compressedSize = (UINT32)m_deflater.getOutputSize(); } // // Write data to socket // { AutoLock l(m_output); m_output->writeUInt32(FTMessage::FILE_LIST_REPLY); m_output->writeUInt8(compressionLevel); m_output->writeUInt32(compressedSize); m_output->writeUInt32(uncompressedSize); if (compressionLevel != 0) { m_output->writeFully(m_deflater.getOutput(), compressedSize); } else { m_output->writeFully(memStream.toByteArray(), uncompressedSize); } m_output->flush(); } // synchronized(m_output) } // void