J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | import java.io.*; |
| 25 | |
| 26 | /* |
| 27 | * Copyright 2003 Wily Technology, Inc. |
| 28 | */ |
| 29 | |
| 30 | public class NamedBuffer |
| 31 | { |
| 32 | private final String fName; |
| 33 | private final byte[] fBuffer; |
| 34 | |
| 35 | public |
| 36 | NamedBuffer( String name, |
| 37 | byte[] buffer) |
| 38 | { |
| 39 | fName = name; |
| 40 | fBuffer = buffer; |
| 41 | } |
| 42 | |
| 43 | public |
| 44 | NamedBuffer( String name, |
| 45 | InputStream stream) |
| 46 | throws IOException |
| 47 | { |
| 48 | this( name, |
| 49 | loadBufferFromStream(stream)); |
| 50 | } |
| 51 | |
| 52 | public String |
| 53 | getName() |
| 54 | { |
| 55 | return fName; |
| 56 | } |
| 57 | |
| 58 | public byte[] |
| 59 | getBuffer() |
| 60 | { |
| 61 | return fBuffer; |
| 62 | } |
| 63 | |
| 64 | public static byte[] |
| 65 | loadBufferFromStream(InputStream stream) |
| 66 | throws IOException |
| 67 | { |
| 68 | // hack for now, just assume the stream will fit in our reasonable size buffer. |
| 69 | // if not, panic |
| 70 | int bufferLimit = 200 * 1024; |
| 71 | byte[] readBuffer = new byte[bufferLimit]; |
| 72 | int actualSize = stream.read(readBuffer); |
| 73 | if ( actualSize >= bufferLimit ) |
| 74 | { |
| 75 | // if there might be more bytes, just surrender |
| 76 | throw new IOException("too big for buffer"); |
| 77 | } |
| 78 | |
| 79 | byte[] resultBuffer = new byte[actualSize]; |
| 80 | System.arraycopy( readBuffer, |
| 81 | 0, |
| 82 | resultBuffer, |
| 83 | 0, |
| 84 | actualSize); |
| 85 | return resultBuffer; |
| 86 | } |
| 87 | } |