Java Byte Array from getBytesFromResource(String resource)

Here you can find the source of getBytesFromResource(String resource)

Description

get Bytes From Resource

License

Apache License

Declaration

public static byte[] getBytesFromResource(String resource)
            throws IOException 

Method Source Code

//package com.java2s;
/*//from   ww  w  .ja  v  a  2 s  .com
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

import java.io.BufferedInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static byte[] getBytesFromResource(String resource)
            throws IOException {
        InputStream is = ClassLoader.getSystemClassLoader()
                .getResourceAsStream(resource);

        try (BufferedInputStream stream = new BufferedInputStream(is)) {
            int len = stream.available();
            byte[] bytes = new byte[len];
            stream.read(bytes, 0, len);
            return bytes;
        }
    }
}

Related

  1. getBytesFromImage(File file)
  2. getBytesFromInt(int value)
  3. getBytesFromList(List values)
  4. getBytesFromObject(Serializable data)
  5. getBytesFromObject(Serializable obj)
  6. getBytesFromStream(int length, ByteArrayInputStream bais)
  7. getBytesFromString(final String str, final int length, final String coding)
  8. getBytesFromText(String text, String charset)
  9. getBytesInCodePage(final String string, final int codepage)

  10. HOME | Copyright © www.java2s.com 2016