KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonathan > apis > resources > Chunk


1 /***
2  * Jonathan: an Open Distributed Processing Environment
3  * Copyright (C) 1999 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Release: 2.0
20  *
21  * Contact: jonathan@objectweb.org
22  *
23  * Author: Bruno Dumant
24  *
25  */

26
27
28 package org.objectweb.jonathan.apis.resources;
29
30 import org.objectweb.jonathan.apis.kernel.JonathanException;
31
32
33 /**
34  * A chunk represents a part of an array of bytes. Chunks are linked to form
35  * messages that may be sent from an adress space to another. Their use avoids
36  * unnecessarily copying arrays of bytes, and helps recovering these arrays
37  * without resorting to garbage collection (thanks to
38  * {@link ChunkFactory chunk factories}).
39  * <p>
40  * Chunks should not be used concurrently.
41  */

42 public class Chunk {
43    /** The associated array of bytes. */
44    public byte[] data;
45    
46    /** Index of the last valid (written) byte in this chunk + 1. */
47    public int top;
48    
49    /** The index of the first valid (written) byte in this chunk. */
50    public int offset;
51
52    /** The next chunk in the chain */
53    public Chunk next;
54    
55    /**
56     * Constructs a new chunk with the specified data and offsets.
57     *
58     * @param data the byte array containing the data.
59     * @param offset the offset of the first valid byte in the chunk.
60     * @param top offset + the number of valid bytes in the chunk.
61     */

62    public Chunk (byte[] data,int offset,int top) {
63       this.data = data;
64       this.offset = offset;
65       this.top = top;
66    }
67
68    /**
69     * Duplicates the whole chunk.
70     * <p>
71     * The default implementation copies the buffer,
72     * and creates a new chunk with it.
73     *
74     * @return a copy of this chunk.
75     * @exception JonathanException if an IO error occurs.
76     */

77    public Chunk duplicate() throws JonathanException {
78       int len = top - offset;
79       byte[] ndata = new byte[len];
80       System.arraycopy(data,offset,ndata,0,len);
81       return new Chunk(ndata,0,len);
82    }
83
84    /**
85     * Partially duplicates this chunk. 'offset' must be greater than
86     * the target chunk's offset, 'top' must be less or equal than the target's top.
87     * <p>
88     * The default implementation copies the appropriate portion of the buffer,
89     * and creates a new chunk with it.
90     *
91     * @param offset the offset of the chunk copy.
92     * @param top the top of the chunk copy.
93     * @return a chunk containing the specified part of the target chunk.
94     * @exception JonathanException if an error occurs.
95     */

96    public Chunk duplicate(int offset,int top) throws JonathanException {
97       int len = top - offset;
98       byte[] ndata = new byte[len];
99       System.arraycopy(data,offset,ndata,0,len);
100       return new Chunk(ndata,0,len);
101    }
102    
103
104    /**
105     * Releases the chunk. The data of a chunk may be obtained from managed
106     * buffers. It may thus be necessary to tell when the data encapsulated by a
107     * chunk may be reused.
108     * <p>
109     * The default implementation resets offset and top to 0.
110     */

111    public void release() {
112       top = 0;
113       offset = 0;
114       next = null;
115    }
116    
117    /**
118     * Returns a string representation of the target chunk.
119     *
120     * @return a string representation of the target chunk.
121     */

122    public String JavaDoc toString() {
123       return "Chunk[data: " + data + " offset: " + offset + " top: " + top + "]";
124    }
125 }
126
127
Popular Tags