【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-ByteArray.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package com.abc.util;
import java.io.*;
import java.net.*;
public class ByteArray
{
/** Returns copy of characters in s as a new byte array. */
public static final byte[] getBytes(String s)
{
/*
byte[] b = null;
try
{
b = s.getBytes(s);
}
catch(UnsupportedEncodingException ex)
{
System.err.println("ByteArry.getBytes(): " + ex);
}
return b;
*/
int len = s.length();
byte b[] = new byte[len];
s.getBytes(0, len, b, 0);
return b;
}
/** Returns contents of file as byte array. */
public static byte[] loadFromFile(String filename) throws IOException
{
return loadFromFile(new File(filename));
}
/** Returns contents of file <i>file</i> as byte array. */
public static byte[] loadFromFile(File file) throws IOException
{
int n, nread = 0, len = (int) file.length();
FileInputStream fin = new FileInputStream(file);
byte[] content = new byte[len];
while (nread < len)
{
if ((n = fin.read(content, nread, len - nread)) == -1)
throw new IOException("Error loading Compound from file");
nread += n;
}
return content;
}
/**
* Reads n bytes from the specified input stream. It will return
* fewer bytes if fewer bytes are available on the stream.
* Hence the application should check the resulting arrays length.
*/
public static byte[] readn(InputStream in, int n) throws IOException
{
byte[] buf = new byte[n];
int ntotal = 0;
int nread;
while (ntotal < n)
{
nread = in.read(buf, ntotal, n - ntotal);
if (nread < 0)
{
// we got less than expected - return what we got
byte[] newbuf = new byte[ntotal];
System.arraycopy(buf, 0, newbuf, 0, ntotal);
return newbuf;
}
ntotal += nread;
}
return buf;
}
/**
* Return contents of a WWW resource identified by a URL.
* @param url the resource to retrieve
* @return the resource contents as a byte array
*/
public static byte[] getContent(URL url) throws IOException
{
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
int length;
/*
* N.B. URLConnection.getContentLength() is buggy for "http" resources
* (at least in JDK1.0.2) and won't work for "file" URLs either.
*/
length = length = conn.getContentLength();
if (length == -1)
length = conn.getHeaderFieldInt("Content-Length", -1);
if (length == -1)
return readAll(in);
return readn(in, length);
}
/**
* Read all input from an InputStream and return as a byte array.
* This method will not return before the end of the stream is reached.
* @return contents of the stream
*/
public static byte[] readAll(InputStream in) throws IOException
{
byte[] buf = new byte[1024];
int nread, ntotal = 0;
while ((nread = in.read(buf, ntotal, buf.length - ntotal)) > -1)
{
ntotal += nread;
if (ntotal == buf.length)
{
// extend buffer
byte[] newbuf = new byte[buf.length * 2];
System.arraycopy(buf, 0, newbuf, 0, buf.length);
buf = newbuf;
}
}
if (ntotal < buf.length)
{
// we cannot have excess space
byte[] newbuf = new byte[ntotal];
System.arraycopy(buf, 0, newbuf, 0, ntotal);
buf = newbuf;
}
return buf;
}
/**
* Copies data from the specified input stream to the output stream
* until end of file is met.
* @return the total number of bytes written to the output stream
*/
public static int cpybytes(InputStream in, OutputStream out)
throws IOException
{
byte[] buf = new byte[1024];
int n, ntotal = 0;
while ((n = in.read(buf)) > -1)
{
out.write(buf, 0, n);
ntotal += n;
}
return ntotal;
}
/**
* Copies data from the specified input stream to the output stream
* until <em>n</em> bytes has been copied or end of file is met.
* @return the total number of bytes written to the output stream
*/
public static int cpybytes(InputStream in, OutputStream out, int n)
throws IOException
{
int sz = n < 1024 ? n : 1024;
byte[] buf = new byte[sz];
int chunk, nread, ntotal = 0;
chunk = sz;
while (ntotal < n && (nread = in.read(buf, 0, chunk)) > -1)
{
out.write(buf, 0, nread);
ntotal += nread;
chunk = (n - ntotal < sz) ? n - ntotal : sz;
}
return ntotal;
}
/**
* Returns the index within this String of the first occurrence of the
* specified character or -1 if the character is not found.
* @params buf the buffer to search
* @params ch the character to search for
*/
public static final int indexOf(byte[] buf,
int ch)
{
return indexOf(buf, ch, 0, buf.length);
}
/**
* Returns the index within this String of the first occurrence of the
* specified character, starti