void AppStateImpl::OnStateLoaded( const jobject pListener, const int iStatusCode, const int iStateKey, const jbyteArray data )
{
    if ( data )
    {
        jbyte* pBytes = m_pEnv->GetByteArrayElements( data, NULL );
        jsize arraySize = m_pEnv->GetArrayLength( data );

        if ( pListener )
        {
            AppStateData stateData( (const char*)pBytes, arraySize );

            IAppStateListener* pStateListener = (IAppStateListener*)m_pEnv->GetDirectBufferAddress( pListener );
            pStateListener->OnStateLoaded( iStatusCode, iStateKey, &stateData );
        }

        m_pEnv->ReleaseByteArrayElements( data, pBytes, JNI_ABORT );
    }
}
bool test_osc_Compartment()
{
  bool test = true;
  osc::byte_t cId[3][3] = {{'1','2','3'},{'1','3','2'},{'1','2','3'}};
  osc::byte_t sData[10] = {'A','B','C','D','E','F','G','H','I','J'};
  osc::StateHandler handler;
  osc::compartment_id_t compartmentId[3];
  osc::Compartment * compartments[3];
  for(int x = 0; x < 3;x++)
  {
    compartmentId[x].copy(cId[x],3);
    compartments[x] = handler.getCompartment(compartmentId[x]);
  }
  
  osc::Compartment * compartment = handler.getCompartment(compartmentId[0]);
  test = test && osc::print_subtest("Compartment Creation", compartment !=0);

  test = test && osc::print_subtest("Compartment Retrieval",
      compartment == handler.getCompartment(compartmentId[0]));
  
  osc::Buffer stateData(sData, 10);
  
  osc::State * state = new osc::State(stateData);
  osc::state_id_t id = state->getId();

  // This routine holds onto the state for the duration of its
  // testing.


  state->retain();
  osc::nack_code_t ns;
  handler.addState(state,compartmentId[0],1);
  state = handler.getState(id, ns);

  osc::State *bigState = 0;
  if(compartment != 0)
  {
    test = test && osc::print_subtest("State Insertion",
        1 == compartment->numberOfStates());

    compartment->removeState(state->getStateId());
    test = test && osc::print_subtest("State Removal",
        compartment->numberOfStates() == 0);
  
    state = handler.getState(id, ns);
    handler.addState(state,compartmentId[0],1);
    test = test && osc::print_subtest("State Re-Insertion",
                      compartment->numberOfStates() == 1);
    
    state = handler.getState(id, ns);
    handler.addState(state,compartmentId[0],1);
    state = handler.getState(id, ns);

    size_t size = compartment->remainingStateMemory();
    if(size >= 65)
    {
      bigState = osc::generateRandomState(
                   compartment->totalStateMemoryAvailable() - 64,false);
      compartment->addState(bigState,1);
      test = test && osc::print_subtest("Inserting Single Large State", 
                                   0 == compartment->remainingStateMemory());
    }
  }
  else
  {
    osc::print_subtest("Multiple Tests Skipped Unable To Continue", false);
  }

  if (state)
  {  
    state->release();
  }
  if (bigState)
  {
    if (compartment)
    {
      compartment->removeState(bigState->getId());
    }
    delete bigState;
  }
  return (test);
}