Android Open Source - SurveySDK_android Serialization Manager






From Project

Back to project page SurveySDK_android.

License

The source code is released under:

Apache License

If you think the Android project SurveySDK_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.survey.android.db;
//from w  ww .  j  ava  2 s  .c  om
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.util.Log;

import com.survey.android.containers.PollContainer;
import com.survey.android.model.AnswerModel;
//import dalvik.system.TemporaryDirectory;

public class SerializationManager {
  public static String TAG = "SERIALIZATION_MANAGER";

  /**
   * Serializes object to byte array ( necessarily object needs to implement
   * Serializable interface )
   * 
   * @param o
   *            - object for serialization
   * @return byte array - representation of object to byte array
   */
  public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
      ObjectOutput out = new ObjectOutputStream(bos);
      out.writeObject(o);
      out.close();
      byte[] buf = bos.toByteArray();
      return buf;
    } catch (IOException ioe) {
      Log.e("serializeObject", "error", ioe);
      return null;
    }
  }

  /**
   * Deserializes byte array to instance of Object ( for further use needs to
   * apply appropriate cast )
   * 
   * @param b
   *            - byte array ( serialized object )
   * @return deserialized instance of Object or null in a case of error
   */
  public static Object deserializeObject(byte[] b) {
    try {
      ObjectInputStream in = new ObjectInputStream(
          new ByteArrayInputStream(b));
      Object object = in.readObject();
      in.close();
      return object;
    } catch (ClassNotFoundException cnfe) {
      Log.e("deserializeObject", "class not found error", cnfe);
      return null;
    } catch (IOException ioe) {
      Log.e("deserializeObject", "io error", ioe);
      return null;
    }
  }

  // Specific for PollContainer class ( only instance of that class is going
  // to be serialized ever )
  // makes things easier but makes a little dirty scope of class

  /**
   * Deserializes byte array to instance of PollContainer class ( uses
   * additional parameter because Context is transient and has value of null
   * when object is deserialized )
   * 
   * @param array
   *            - byte array representation of object
   * @param context
   *            - set field from PollContainer to value context ( current
   *            activity connected to pollContainer )
   * @return instance of PollContainer class or null in a case of error
   */
  public static PollContainer deserializePollContainer(byte[] array,
      Context context) {
    try {
      PollContainer tempContainer = (PollContainer) deserializeObject(array);
      if (tempContainer != null) {
        tempContainer.setContext(context);
      }
      // *************************************************************************************************
      List<AnswerModel> tempList = tempContainer.getAnswers();
      if (tempList != null) {
        Set<String> set = new HashSet<String>();
        for (AnswerModel a : tempList) {
          if (!a.getQuestionId().equals("token")) {
            set.add(a.getQuestionId());
          }
        }
        
        int tempCursor = tempContainer.getCursor();
        if (set.size() > 0 && tempCursor >= set.size()) {
          tempContainer.setCursor(set.size() - 1);
        }
        tempContainer.getSection().setType("restored");
      }
      // *************************************************************************************************
      return tempContainer;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Dashboard.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.Notification.java
com.survey.android.view.Question.java
com.survey.android.view.Survey.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.DashboardThemed.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.NotificationThemed.java
com.survey.android.view.themed.QuestionThemed.java
com.survey.android.view.themed.SurveyThemed.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java