Subversion Repository Public Repository

TransPort_Tracking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bestray.transtracking.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 *
 * @author Girija Sahu
 */
public class PreferenceUtil {
   
// Max byte count is 3/4 max string length (see Preferences
  // documentation).
  static private final int pieceLength =
    ((3*Preferences.MAX_VALUE_LENGTH)/4);

  static private byte[] object2Bytes( Object o ) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream( baos );
    oos.writeObject( o );
    return baos.toByteArray();
  }

  static private byte[][] breakIntoPieces( byte raw[] ) {
    int numPieces = (raw.length + pieceLength - 1) / pieceLength;
    byte pieces[][] = new byte[numPieces][];
    for (int i=0; i<numPieces; ++i) {
      int startByte = i * pieceLength;
      int endByte = startByte + pieceLength;
      if (endByte > raw.length) endByte = raw.length;
      int length = endByte - startByte;
      pieces[i] = new byte[length];
      System.arraycopy( raw, startByte, pieces[i], 0, length );
    }
    return pieces;
  }

  static private void writePieces( Preferences prefs, String key,
      byte pieces[][] ) throws BackingStoreException {
    Preferences node = prefs.node( key );
    node.clear();
    for (int i=0; i<pieces.length; ++i) {
      node.putByteArray( ""+i, pieces[i] );
    }
  }

  static private byte[][] readPieces( Preferences prefs, String key )
      throws BackingStoreException {
    Preferences node = prefs.node( key );
    String keys[] = node.keys();
    int numPieces = keys.length;
    byte pieces[][] = new byte[numPieces][];
    for (int i=0; i<numPieces; ++i) {
      pieces[i] = node.getByteArray( ""+i, null );
    }
    return pieces;
  }

  static private byte[] combinePieces( byte pieces[][] ) {
    int length = 0;
    for (int i=0; i<pieces.length; ++i) {
      length += pieces[i].length;
    }
    byte raw[] = new byte[length];
    int cursor = 0;
    for (int i=0; i<pieces.length; ++i) {
      System.arraycopy( pieces[i], 0, raw, cursor, pieces[i].length );
      cursor += pieces[i].length;
    }
    return raw;
  }

  static private Object bytes2Object( byte raw[] )
      throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream( raw );
    ObjectInputStream ois = new ObjectInputStream( bais );
    Object o = ois.readObject();
    return o;
  }

  static public void putObject( Preferences prefs, String key, Object o )
      throws IOException, BackingStoreException, ClassNotFoundException {
    byte raw[] = object2Bytes( o );
    byte pieces[][] = breakIntoPieces( raw );
    writePieces( prefs, key, pieces );
  }

  static public Object getObject( Preferences prefs, String key )
      throws IOException, BackingStoreException, ClassNotFoundException {
    byte pieces[][] = readPieces( prefs, key );
    byte raw[] = combinePieces( pieces );
    Object o = bytes2Object( raw );
    return o;
  }
}

Commits for TransPort_Tracking/TransPortTracking/src/main/java/com/bestray/transtracking/util/PreferenceUtil.java

Diff revisions: vs.
Revision Author Commited Message
1 girijabapi picture girijabapi Sat 28 Jul, 2018 05:29:14 +0000