KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > asn1 > BEROutputStream


1 /*_############################################################################
2   _##
3   _## SNMP4J - BEROutputStream.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23
24 package org.snmp4j.asn1;
25
26 import java.io.*;
27 import java.nio.ByteBuffer JavaDoc;
28
29
30 /**
31  * The <code>BEROutputStream</code> class wraps a <code>ByteBuffer</code>
32  * to support BER encoding. The backing buffer can be accessed directly to
33  * optimize performance and memory usage.
34  *
35  * @author Frank Fock
36  * @version 1.0
37  */

38 public class BEROutputStream extends OutputStream {
39
40   private ByteBuffer JavaDoc buffer;
41   private int offset = 0;
42
43   /**
44    * Creates a BER output stream without a backing buffer set. In order to
45    * be able to write anything to the stream,
46    * {@link #setBuffer(ByteBuffer buffer)} has to be
47    * called before. Otherwise a <code>NullPointerException</code> will be
48    * thrown when calling one of the <code>write</code> operations.
49    */

50   public BEROutputStream() {
51     this.buffer = null;
52   }
53
54   /**
55    * Create a <code>BEROutputStream</code> that uses the supplied buffer
56    * as backing buffer.
57    * @param buffer
58    * a <code>ByteBuffer</code> whose limit and capacity must be greater or
59    * equal that the length of the encoded BER stream.
60    */

61   public BEROutputStream(ByteBuffer JavaDoc buffer) {
62     this.buffer = buffer;
63     this.offset = buffer.position();
64   }
65
66   public void write(int b) throws java.io.IOException JavaDoc {
67     buffer.put((byte)b);
68   }
69
70   public void write(byte[] b) throws IOException {
71     buffer.put(b);
72   }
73
74   public void write(byte[] b, int off, int len) throws IOException {
75       buffer.put(b, off, len);
76   }
77
78   public void close() throws IOException {
79   }
80
81   public void flush() throws IOException {
82   }
83
84   /**
85    * Rewinds backing buffer and returns it. In contrast to the backing buffer's
86    * rewind method, this method sets the position of the buffer to the first
87    * byte of this output stream rather than to the first byte of the underlying
88    * byte array!
89    * @return
90    * the ByteBuffer backing this output stream with its current position
91    * set to the begin of the output stream.
92    */

93   public ByteBuffer JavaDoc rewind() {
94     return (ByteBuffer JavaDoc) buffer.position(offset);
95   }
96
97   /**
98    * Gets the backing buffer.
99    * @return
100    * the <code>ByteBuffer</code> backing this output stream.
101    */

102   public ByteBuffer JavaDoc getBuffer() {
103     return buffer;
104   }
105
106   /**
107    * Sets the backing buffer to the supplied one and sets the offset used by
108    * {@link #rewind()} to the buffers current position.
109    * @param buffer
110    * a <code>ByteBuffer</code> whose limit and capacity must be greater or
111    * equal that the length of the encoded BER stream.
112    */

113   public void setBuffer(ByteBuffer JavaDoc buffer) {
114     this.buffer = buffer;
115     this.offset = buffer.position();
116   }
117
118   /**
119    * Sets the backing buffer and sets the current position of the stream to
120    * the buffers limit (end).
121    * @param buffer
122    * a <code>ByteBuffer</code> whose limit and capacity must be greater or
123    * equal that the length of the encoded BER stream.
124    */

125   public void setFilledBuffer(ByteBuffer JavaDoc buffer) {
126     this.buffer = buffer;
127     this.offset = buffer.position();
128     buffer.position(buffer.limit());
129   }
130
131 }
132
Popular Tags