1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: Record.java 408 2006-01-02 08:47:23Z mat007 $ 29 */ 30 31 package palmed.io; 32 33 import java.io.ByteArrayInputStream; 34 import java.io.ByteArrayOutputStream; 35 import java.io.DataInputStream; 36 import java.io.DataOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.io.OutputStream; 40 import javax.microedition.rms.RecordStore; 41 import javax.microedition.rms.RecordStoreException; 42 43 /*** 44 * This class handles serialization as a record. 45 * 46 * @author Mathieu Champlon 47 * @version $Revision: 408 $ $Date: 2006-01-02 17:47:23 +0900 (lun., 02 janv. 2006) $ 48 */ 49 public final class Record implements IRecord 50 { 51 /*** 52 * The record store. 53 */ 54 private final RecordStore store_; 55 /*** 56 * The buffer. 57 */ 58 private final ISerializable serializable_; 59 /*** 60 * The record identifier. 61 */ 62 private int recordId_; 63 64 /*** 65 * Create a buffer record. 66 * 67 * @param store the record store 68 * @param serializable the object to serialize 69 */ 70 public Record( final RecordStore store, final ISerializable serializable ) 71 { 72 this( store, serializable, -1 ); 73 } 74 75 /*** 76 * Create a record from a given identifier. 77 * 78 * @param store the record store 79 * @param serializable the object to serialize 80 * @param recordId the record identifier 81 */ 82 public Record( final RecordStore store, final ISerializable serializable, final int recordId ) 83 { 84 if( store == null ) 85 throw new IllegalArgumentException( "parameter 'store' is null" ); 86 if( serializable == null ) 87 throw new IllegalArgumentException( "parameter 'serializable' is null" ); 88 store_ = store; 89 serializable_ = serializable; 90 recordId_ = recordId; 91 } 92 93 /*** 94 * {@inheritDoc} 95 */ 96 public void persist() 97 { 98 try 99 { 100 final ByteArrayOutputStream stream = new ByteArrayOutputStream(); 101 serializable_.marshall( stream ); 102 final byte[] data = stream.toByteArray(); 103 if( recordId_ == -1 ) 104 recordId_ = store_.addRecord( data, 0, data.length ); 105 else 106 store_.setRecord( recordId_, data, 0, data.length ); 107 } 108 catch( IOException e ) 109 { 110 throw new RecordException( e ); 111 } 112 catch( RecordStoreException e ) 113 { 114 throw new RecordException( e ); 115 } 116 } 117 118 /*** 119 * {@inheritDoc} 120 */ 121 public void restore() 122 { 123 try 124 { 125 if( recordId_ != -1 ) 126 { 127 final byte[] data = store_.getRecord( recordId_ ); 128 if( data != null ) 129 serializable_.unmarshall( new ByteArrayInputStream( data ) ); 130 } 131 } 132 catch( IOException e ) 133 { 134 throw new RecordException( e ); 135 } 136 catch( RecordStoreException e ) 137 { 138 throw new RecordException( e ); 139 } 140 } 141 142 /*** 143 * {@inheritDoc} 144 */ 145 public void delete() 146 { 147 try 148 { 149 if( recordId_ != -1 ) 150 { 151 store_.deleteRecord( recordId_ ); 152 recordId_ = -1; 153 } 154 } 155 catch( RecordStoreException e ) 156 { 157 throw new RecordException( e ); 158 } 159 } 160 161 /*** 162 * {@inheritDoc} 163 */ 164 public void marshall( final OutputStream stream ) throws IOException 165 { 166 final DataOutputStream output = new DataOutputStream( stream ); 167 output.writeInt( recordId_ ); 168 } 169 170 /*** 171 * {@inheritDoc} 172 */ 173 public void unmarshall( final InputStream stream ) throws IOException 174 { 175 final DataInputStream input = new DataInputStream( stream ); 176 recordId_ = input.readInt(); 177 } 178 }