Ahojte,
On 10/31/07, Ján Valkovič <[EMAIL PROTECTED]> wrote:
> ja som potreboval nacitat stored proceduru z jar-ka, co je cosi podobne.
To
> nacitanie som riesil takto:
...
> byte[] b = new byte[inputStream.available()];
...
Na available() bych nespoléhal. Nemusí vrátit celkovou velikost:
Returns the number of bytes that can be read from this file input stream
without blocking.
> ya
Já to řeším ve třídě StringUtils (část převzata z Apache commons)
následovně:
/**
* The name says it all.
*/
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* Copy bytes from an <code>InputStream</code> to an
<code>OutputStream</code>.
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws IOException In case of an I/O problem
*/
public static int copy(
InputStream input,
OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
* Returns InputStream as a String
* @param aIS input stream
* @param aEncoding character set name (or null for the system default)
*/
public static String stream2String(final InputStream aIS, final String
aEncoding) {
if (aIS == null) {
return null;
}
ByteArrayOutputStream tmpOS = new ByteArrayOutputStream();
String tmpResult = null;
try {
copy(aIS, tmpOS);
if (aEncoding!=null && Charset.isSupported(aEncoding)) {
tmpResult = tmpOS.toString(aEncoding);
} else {
tmpResult = tmpOS.toString();
}
} catch (IOException e) {
//nothing to do
}
return tmpResult;
}
/**
* Returns file content as a String
* @param aFileName file name
* @param aEncoding character set name (or null for the system default)
*/
public static String file2String(final String aFileName, final String
aEncoding) {
if (aFileName == null) {
return null;
}
final File tmpFile = new File(aFileName);
InputStream tmpIS = null;
if (tmpFile.exists()) {
try {
tmpIS = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
// should never happen
}
} else {
tmpIS = StringUtils.class.getClassLoader
().getResourceAsStream(aFileName);
}
return stream2String(tmpIS, aEncoding);
}
a potom volám
StringUtils.file2string("adresar/mujtext.txt", "windows-1250");
-- pepa cacek