Advertisement
Guest User

Untitled

a guest
Feb 16th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. public class XMLHandler
  2. {
  3.     public static <T> boolean writeSerialFile(T writeable, String fileName)
  4.     {
  5.         if (hasStorage(true))
  6.         {
  7.             // write
  8.             Serializer serial = new Persister();
  9.            
  10.             File sdCard = Environment.getExternalStorageDirectory();
  11.             File dir = new File(sdCard.getAbsolutePath() + "/rfr");
  12.             dir.mkdirs();
  13.            
  14.             File sdcardFile = new File(dir, fileName + ".xml");
  15.             try
  16.             {
  17.                 if (sdcardFile.exists())
  18.                     sdcardFile.delete();
  19.                
  20.                 sdcardFile.createNewFile();
  21.             }
  22.             catch (IOException e)
  23.             {
  24.                 Log.e("JAKOBERR", e.toString());
  25.             }
  26.            
  27.             try
  28.             {
  29.                 serial.write(writeable, sdcardFile);
  30.                 return true;
  31.             }
  32.             catch (Exception e)
  33.             {
  34.                 // There is the possibility of error for a number of reasons.
  35.                 // Handle
  36.                 // this appropriately in your code
  37.                 Log.e("JAKOBERR", e.toString());
  38.             }
  39.         }
  40.        
  41.         return false;
  42.     }
  43.    
  44.     private static boolean hasStorage(boolean requireWriteAccess)
  45.     {
  46.         String state = Environment.getExternalStorageState();
  47.        
  48.         if (Environment.MEDIA_MOUNTED.equals(state))
  49.         {
  50.             return true;
  51.         }
  52.         else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
  53.         {
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     public static <T> T readSerialFile(String fileName, Class<? extends T> type)
  60.     {
  61.         T finalReturn = null;
  62.         Serializer serial = new Persister();
  63.        
  64.         if (hasStorage(false))
  65.         {
  66.             File sdCard = Environment.getExternalStorageDirectory();
  67.             File dir = new File(sdCard.getAbsolutePath() + "/rfr");
  68.            
  69.             File sdcardFile = new File(dir, fileName + ".xml");
  70.             try
  71.             {
  72.                 if (sdcardFile.exists())
  73.                 {
  74.                     byte[] temp = ioStremtoByteArray(new FileInputStream(sdcardFile));
  75.                     String temp2 = new String(temp, "UTF-8");
  76.                     try
  77.                     {
  78.                         finalReturn = serial.read(type, temp2);
  79.                     }
  80.                     catch (Exception e)
  81.                     {
  82.                         Log.e("JAKOBERR", e.toString());
  83.                     }  
  84.                 }
  85.             }
  86.             catch (IOException e)
  87.             {
  88.                 Log.e("JAKOBERR", e.toString());
  89.             }
  90.         }
  91.        
  92.         return finalReturn;
  93.     }
  94.    
  95.     public static <T> T readSerialFile(Resources resources, int identity, Class<? extends T> type)
  96.     {
  97.         T finalReturn = null;
  98.         Serializer serial = new Persister();
  99.        
  100.         try
  101.         {
  102.             byte[] temp = ioStremtoByteArray(resources.openRawResource(identity));
  103.             String temp2 = new String(temp, "UTF-8");
  104.             finalReturn = serial.read(type, temp2);
  105.         }
  106.         catch (NotFoundException e)
  107.         {
  108.             Log.e("JAKOBERR", e.toString());
  109.         }
  110.         catch (Exception e)
  111.         {
  112.             Log.e("JAKOBERR", e.toString());
  113.         }
  114.        
  115.         return finalReturn;
  116.     }
  117.    
  118.     public static byte[] ioStremtoByteArray(InputStream is)
  119.     {
  120.         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  121.        
  122.         int nRead;
  123.         byte[] data = new byte[16384];
  124.        
  125.         try
  126.         {
  127.             while ((nRead = is.read(data, 0, data.length)) != -1)
  128.             {
  129.                 buffer.write(data, 0, nRead);
  130.             }
  131.         }
  132.         catch (IOException e)
  133.         {
  134.             Log.e("JAKOBERR", e.toString());
  135.         }
  136.        
  137.         try
  138.         {
  139.             buffer.flush();
  140.         }
  141.         catch (IOException e)
  142.         {
  143.             Log.e("JAKOBERR", e.toString());
  144.         }
  145.        
  146.         return buffer.toByteArray();
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement