List of usage examples for java.io IOException IOException
public IOException(Throwable cause)
From source file:com.google.android.feeds.ContentHandlerUtils.java
/** * Returns the character set of the content provided by the given * {@link URLConnection}.// ww w .j av a 2 s . com * * @throws IOException if the character set cannot be determined. */ public static String getCharSet(URLConnection connection) throws IOException { String contentType = connection.getContentType(); if (contentType != null) { HeaderValueParser parser = new BasicHeaderValueParser(); HeaderElement[] values = BasicHeaderValueParser.parseElements(contentType, parser); if (values.length > 0) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { return param.getValue(); } } } if (connection instanceof HttpURLConnection) { return HTTP.DEFAULT_CONTENT_CHARSET; } else { throw new IOException("Unabled to determine character encoding"); } }
From source file:acromusashi.kafka.log.producer.util.YamlReadUtil.java
/** * ???Yaml????Yaml?/*from w w w.ja v a2 s. co m*/ * * @param filePath * @return ??? * @throws IOException */ @SuppressWarnings({ "unchecked" }) public static Map<String, Object> readYaml(String filePath) throws IOException { Map<String, Object> configObject = null; Yaml yaml = new Yaml(); InputStream inputStream = null; InputStreamReader steamReader = null; File file = new File(filePath); try { inputStream = new FileInputStream(file.getAbsolutePath()); steamReader = new InputStreamReader(inputStream, "UTF-8"); configObject = (Map<String, Object>) yaml.load(steamReader); } catch (Exception ex) { // ScannerException/IOException????? // 2???????????????Exception????? throw new IOException(ex); } finally { IOUtils.closeQuietly(inputStream); } return configObject; }
From source file:com.symbian.driver.engine.Utils.java
/** * @param aCmd/*ww w . j a v a 2s. c o m*/ * @param aFirstParameter * @param aSecondParameter * @return * @throws JStatException * @throws IOException * @throws TimeLimitExceededException */ public static final BufferedReader executeOnDevice(final int aCmd, final String aFirstParameter, final String aSecondParameter) throws JStatException, IOException, TimeLimitExceededException { PipedOutputStream lPipedOutputStream = new PipedOutputStream(); PipedInputStream lPipedInputStream = new PipedInputStream(lPipedOutputStream); try { if (aFirstParameter == null && aSecondParameter == null) { new ExecuteOnDevice(aCmd).doTask(true, EngineTests.TIMEOUT, false); } else if (aFirstParameter != null && aSecondParameter == null) { new ExecuteOnDevice(aCmd, aFirstParameter).doTask(true, EngineTests.TIMEOUT, false); } else if (aFirstParameter != null && aSecondParameter != null) { new ExecuteOnDevice(aCmd, aFirstParameter, aSecondParameter).doTask(true, EngineTests.TIMEOUT, false); } } catch (ParseException lParseException) { throw new IOException("Could not read transport. " + lParseException.getMessage()); } BufferedReader lBufferedReader = new BufferedReader(new InputStreamReader(lPipedInputStream)); // lPipedInputStream.close(); lPipedOutputStream.close(); return lBufferedReader; }
From source file:de.codesourcery.eve.skills.util.ClasspathInputStreamProvider.java
@Override public InputStream createInputStream() throws IOException { final InputStream result = ClasspathInputStreamProvider.class.getResourceAsStream(path); if (result == null) { throw new IOException("Resource not found on classpath: " + path); }/*from w ww.j a va 2s . c o m*/ return result; }
From source file:Main.java
/** * Reads the contents of an InputStream into a byte[]. * *///from w ww . j a va 2s . c o m public static byte[] streamToBytes(InputStream in, int length) throws IOException { byte[] bytes = new byte[length]; int count; int pos = 0; while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) { pos += count; } if (pos != length) { throw new IOException("Expected " + length + " bytes, read " + pos + " bytes"); } return bytes; }
From source file:hudson.util.SignatureOutputStream.java
@Override public void write(int b) throws IOException { try {/* w ww. ja v a2s . com*/ sig.update((byte) b); out.write(b); } catch (SignatureException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } }
From source file:io.milton.common.RangeUtils.java
public static void writeRanges(InputStream in, List<Range> ranges, OutputStream responseOut) throws IOException { try {/* w ww . java 2 s . c o m*/ InputStream bufIn = in; //new BufferedInputStream(in); long pos = 0; for (Range r : ranges) { long skip = r.getStart() - pos; bufIn.skip(skip); Long length = r.getLength(); if (length == null) { // will return null if cant calculate throw new IOException( "Unable to write range because either start or finish index are not provided: " + r); } sendBytes(bufIn, responseOut, length); pos = r.getFinish(); } } finally { StreamUtils.close(in); } }
From source file:edu.iu.kmeans.regroupallgather.KMUtil.java
/** * Generate centroids and upload to the cDir * /*from w ww. ja va 2 s . c o m*/ * @param numCentroids * @param vectorSize * @param configuration * @param random * @param cenDir * @param fs * @throws IOException */ static void generateCentroids(int numCentroids, int vectorSize, Configuration configuration, Path cenDir, FileSystem fs) throws IOException { Random random = new Random(); double[] data = null; if (fs.exists(cenDir)) fs.delete(cenDir, true); if (!fs.mkdirs(cenDir)) { throw new IOException("Mkdirs failed to create " + cenDir.toString()); } data = new double[numCentroids * vectorSize]; for (int i = 0; i < data.length; i++) { // data[i] = 1000; data[i] = random.nextDouble() * 1000; } Path initClustersFile = new Path(cenDir, Constants.CENTROID_FILE_NAME); System.out.println("Generate centroid data." + initClustersFile.toString()); FSDataOutputStream out = fs.create(initClustersFile, true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); for (int i = 0; i < data.length; i++) { if ((i % vectorSize) == (vectorSize - 1)) { bw.write(data[i] + ""); bw.newLine(); } else { bw.write(data[i] + " "); } } bw.flush(); bw.close(); System.out.println("Wrote centroids data to file"); }
From source file:net.przemkovv.sphinx.Utils.java
public static File createRandomTempDirectory(String prefix, String tmp_dir) throws IOException { final File temp; File tmp_dir_file = null;//from w w w. j a v a 2 s . c o m if (tmp_dir != null && !tmp_dir.isEmpty()) { tmp_dir_file = new File(tmp_dir); } temp = File.createTempFile(prefix, null, tmp_dir_file); if (!(temp.delete())) { throw new IOException("Could not delete temp file: " + temp.getAbsolutePath()); } if (!(temp.mkdir())) { throw new IOException("Could not create temp directory: " + temp.getAbsolutePath()); } return (temp); }
From source file:com.hadoop.compression.lzo.LzopOutputStream.java
/** * Write an lzop-compatible header to the OutputStream provided. *///from w ww . j a v a 2 s .co m protected static void writeLzopHeader(OutputStream out, LzoCompressor.CompressionStrategy strategy) throws IOException { DataOutputBuffer dob = new DataOutputBuffer(); try { dob.writeShort(LzopCodec.LZOP_VERSION); dob.writeShort(LzoCompressor.LZO_LIBRARY_VERSION); dob.writeShort(LzopCodec.LZOP_COMPAT_VERSION); switch (strategy) { case LZO1X_1: dob.writeByte(1); dob.writeByte(5); break; case LZO1X_15: dob.writeByte(2); dob.writeByte(1); break; case LZO1X_999: dob.writeByte(3); dob.writeByte(9); break; default: throw new IOException("Incompatible lzop strategy: " + strategy); } dob.writeInt(0); // all flags 0 dob.writeInt(0x81A4); // mode dob.writeInt((int) (System.currentTimeMillis() / 1000)); // mtime dob.writeInt(0); // gmtdiff ignored dob.writeByte(0); // no filename Adler32 headerChecksum = new Adler32(); headerChecksum.update(dob.getData(), 0, dob.getLength()); int hc = (int) headerChecksum.getValue(); dob.writeInt(hc); out.write(LzopCodec.LZO_MAGIC); out.write(dob.getData(), 0, dob.getLength()); } finally { dob.close(); } }