Merge "Fix for Bug 495."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / NamedByteArrayInputStream.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.parser.util;
10
11 import org.apache.commons.io.IOUtils;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16
17 public class NamedByteArrayInputStream extends ByteArrayInputStream implements NamedInputStream {
18     private final String toString;
19     private NamedByteArrayInputStream(byte[] buf, String toString) {
20         super(buf);
21         this.toString = toString;
22     }
23
24     public static ByteArrayInputStream create(InputStream originalIS) throws IOException {
25         String content = IOUtils.toString(originalIS);
26         if (originalIS instanceof NamedInputStream) {
27             return new NamedByteArrayInputStream(content.getBytes(), originalIS.toString());
28         } else {
29             return new ByteArrayInputStream(content.getBytes());
30         }
31     }
32
33     @Override
34     public String toString() {
35         return toString;
36     }
37 }