001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.protocol;
019    
020    import org.otfeed.support.BufferFormat;
021    
022    import java.nio.ByteBuffer;
023    
024    /**
025     * Runtime exception type to be thrown when a protocol violation occurs.
026     * This exception holds the frame that caused the problem. The frame content
027     * will be printed if exception is printed.
028     */
029    public class ProtocolException extends RuntimeException {
030    
031            private static final long serialVersionUID = -1858209629779954840L;
032    
033            private final ByteBuffer buffer;
034    
035            public ProtocolException(String reason, ByteBuffer bb) {
036                    super(reason);
037                    buffer = bb;
038            }
039    
040            @Override
041            public String toString() {
042    
043                    String out = "ProtocolException: " + getMessage() + "\n";
044    
045                    if(buffer != null) {
046                            BufferFormat format = new BufferFormat();
047                            out += "buffer: " + buffer;
048                            buffer.position(0);
049                            out += "\n";
050                            out += format.format(buffer);
051                    }
052    
053                    return out;
054            }
055    }