001: /*_############################################################################
002: _##
003: _## SNMP4J - BEROutputStream.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.asn1;
022:
023: import java.io.*;
024: import java.nio.ByteBuffer;
025:
026: /**
027: * The <code>BEROutputStream</code> class wraps a <code>ByteBuffer</code>
028: * to support BER encoding. The backing buffer can be accessed directly to
029: * optimize performance and memory usage.
030: *
031: * @author Frank Fock
032: * @version 1.0
033: */
034: public class BEROutputStream extends OutputStream {
035:
036: private ByteBuffer buffer;
037: private int offset = 0;
038:
039: /**
040: * Creates a BER output stream without a backing buffer set. In order to
041: * be able to write anything to the stream,
042: * {@link #setBuffer(ByteBuffer buffer)} has to be
043: * called before. Otherwise a <code>NullPointerException</code> will be
044: * thrown when calling one of the <code>write</code> operations.
045: */
046: public BEROutputStream() {
047: this .buffer = null;
048: }
049:
050: /**
051: * Create a <code>BEROutputStream</code> that uses the supplied buffer
052: * as backing buffer.
053: * @param buffer
054: * a <code>ByteBuffer</code> whose limit and capacity must be greater or
055: * equal that the length of the encoded BER stream.
056: */
057: public BEROutputStream(ByteBuffer buffer) {
058: this .buffer = buffer;
059: this .offset = buffer.position();
060: }
061:
062: public void write(int b) throws java.io.IOException {
063: buffer.put((byte) b);
064: }
065:
066: public void write(byte[] b) throws IOException {
067: buffer.put(b);
068: }
069:
070: public void write(byte[] b, int off, int len) throws IOException {
071: buffer.put(b, off, len);
072: }
073:
074: public void close() throws IOException {
075: }
076:
077: public void flush() throws IOException {
078: }
079:
080: /**
081: * Rewinds backing buffer and returns it. In contrast to the backing buffer's
082: * rewind method, this method sets the position of the buffer to the first
083: * byte of this output stream rather than to the first byte of the underlying
084: * byte array!
085: * @return
086: * the ByteBuffer backing this output stream with its current position
087: * set to the begin of the output stream.
088: */
089: public ByteBuffer rewind() {
090: return (ByteBuffer) buffer.position(offset);
091: }
092:
093: /**
094: * Gets the backing buffer.
095: * @return
096: * the <code>ByteBuffer</code> backing this output stream.
097: */
098: public ByteBuffer getBuffer() {
099: return buffer;
100: }
101:
102: /**
103: * Sets the backing buffer to the supplied one and sets the offset used by
104: * {@link #rewind()} to the buffers current position.
105: * @param buffer
106: * a <code>ByteBuffer</code> whose limit and capacity must be greater or
107: * equal that the length of the encoded BER stream.
108: */
109: public void setBuffer(ByteBuffer buffer) {
110: this .buffer = buffer;
111: this .offset = buffer.position();
112: }
113:
114: /**
115: * Sets the backing buffer and sets the current position of the stream to
116: * the buffers limit (end).
117: * @param buffer
118: * a <code>ByteBuffer</code> whose limit and capacity must be greater or
119: * equal that the length of the encoded BER stream.
120: */
121: public void setFilledBuffer(ByteBuffer buffer) {
122: this.buffer = buffer;
123: this.offset = buffer.position();
124: buffer.position(buffer.limit());
125: }
126:
127: }
|