View Javadoc

1   /*
2    * $Id: DefaultChunk.java,v 1.2 2005/11/01 00:39:05 weiju Exp $
3    * 
4    * Created on 2005/09/23
5    * Copyright 2005 by Wei-ju Wu
6    *
7    * This file is part of The Z-machine Preservation Project (ZMPP).
8    *
9    * ZMPP is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   *
14   * ZMPP is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with ZMPP; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22   */
23  package org.zmpp.iff;
24  
25  import org.zmpp.base.MemoryAccess;
26  
27  /***
28   * This is the default implementation of the Chunk interface.
29   * 
30   * @author Wei-ju Wu
31   * @version 1.0
32   */
33  public class DefaultChunk implements Chunk {
34  
35    /***
36     * The memory access object.
37     */
38    protected MemoryAccess memaccess;
39    
40    /***
41     * The chunk id.
42     */
43    private byte[] id;
44    
45    /***
46     * The chunk size.
47     */
48    private int chunkSize;
49    
50    /***
51     * Constructor.
52     * 
53     * @param memaccess a memory access object to the chunk data
54     */
55    public DefaultChunk(MemoryAccess memaccess) {
56      
57      this.memaccess = memaccess;
58      initBaseInfo();
59    }
60    
61    /***
62     * Initialize the base information for this chunk. 
63     */
64    private void initBaseInfo() {
65      
66      // Determine the chunk id
67      id = new byte[CHUNK_ID_LENGTH];
68      for (int i = 0; i < CHUNK_ID_LENGTH; i++) {
69        
70        id[i] = memaccess.readByte(i);
71      }
72      
73      // Determine the chunk size 
74      chunkSize = (int) memaccess.readUnsigned32(CHUNK_ID_LENGTH);    
75    }
76    
77    /***
78     * {@inheritDoc}
79     */
80    public boolean isValid() {
81      
82      return true;
83    }
84  
85    /***
86     * {@inheritDoc}
87     */
88    public byte[] getId() {
89      
90      return id;
91    }
92  
93    /***
94     * {@inheritDoc}
95     */
96    public int getSize() {
97      
98      return chunkSize;
99    }
100   
101   /***
102    * {@inheritDoc}
103    */
104   public MemoryAccess getMemoryAccess() {
105     
106     return memaccess;
107   }
108 }