Example usage for java.io IOException getLocalizedMessage

List of usage examples for java.io IOException getLocalizedMessage

Introduction

In this page you can find the example usage for java.io IOException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input files.//from   ww w .j a  v a  2  s .  c om
 * 
 * @param outputDir
 *            The temporary directory where the zip files.
 * @param zipFileBaseName
 *            The name of the zip file.
 * @param files
 *            The array files to zip.
 * @return The zip file or null.
 * @throws IOException
 */
public static File zip(final File outputDir, final String zipFileBaseName, final File[] files)
        throws IOException {

    if (outputDir != null && files != null && zipFileBaseName != null) {

        // //////////////////////////////////////////
        // Create a buffer for reading the files
        // //////////////////////////////////////////
        final File outZipFile = new File(outputDir, zipFileBaseName + ".zip");
        ZipOutputStream out = null;

        try {

            // /////////////////////////////////
            // Create the ZIP output stream
            // /////////////////////////////////

            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outZipFile)));

            // /////////////////////
            // Compress the files
            // /////////////////////

            for (File file : files) {
                if (file.isDirectory()) {
                    zipDirectory(file, file, out);
                } else {
                    zipFile(file, out);
                }
            }

            out.close();
            out = null;

            return outZipFile;

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            return null;
        } finally {
            if (out != null)
                out.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input directory.
 * //from w  ww .j a  v a2s.c  o  m
 * @param directory
 *            The directory to be zipped.
 * @param base
 *            The base directory.
 * @param out
 *            The zip output stream.
 * @throws IOException
 */
public static void zipDirectory(final File directory, final File base, final ZipOutputStream out)
        throws IOException {

    if (directory != null && base != null && out != null) {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[4096];
        int read = 0;

        FileInputStream in = null;
        ZipEntry entry = null;

        try {
            for (int i = 0, n = files.length; i < n; i++) {
                if (files[i].isDirectory()) {
                    zipDirectory(files[i], base, out);
                } else {
                    in = new FileInputStream(files[i]);
                    entry = new ZipEntry(base.getName().concat("\\")
                            .concat(files[i].getPath().substring(base.getPath().length() + 1)));
                    out.putNextEntry(entry);

                    while (-1 != (read = in.read(buffer))) {
                        out.write(buffer, 0, read);
                    }

                    // //////////////////////
                    // Complete the entry
                    // //////////////////////

                    out.closeEntry();

                    in.close();
                    in = null;
                }
            }

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            if (out != null)
                out.close();
        } finally {
            if (in != null)
                in.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input file./*  w  ww. j  a  va 2  s . c  om*/
 * 
 * @param file
 *            The input file to be zipped.
 * @param out
 *            The zip output stream.
 * @throws IOException
 */
public static void zipFile(final File file, final ZipOutputStream out) throws IOException {

    if (file != null && out != null) {

        // ///////////////////////////////////////////
        // Create a buffer for reading the files
        // ///////////////////////////////////////////

        byte[] buf = new byte[4096];

        FileInputStream in = null;

        try {
            in = new FileInputStream(file);

            // //////////////////////////////////
            // Add ZIP entry to output stream.
            // //////////////////////////////////

            out.putNextEntry(new ZipEntry(FilenameUtils.getName(file.getAbsolutePath())));

            // //////////////////////////////////////////////
            // Transfer bytes from the file to the ZIP file
            // //////////////////////////////////////////////

            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            // //////////////////////
            // Complete the entry
            // //////////////////////

            out.closeEntry();

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            if (out != null)
                out.close();
        } finally {
            if (in != null)
                in.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

/**
 * Converts the protobuf message to JSON (wrapping exceptions).
 *
 * @param message Protobuf message./*from ww w.  ja  v  a  2 s.c  o  m*/
 * @param <T> Type of protobuf.
 * @return JSON string representation of protobuf.
 * @throws RuntimeException If an exception occurs.
 */
public static <T extends Message> String protobufToJson(T message) throws RuntimeException {
    try {
        return JsonFormat.writeAsString(message);
    } catch (IOException e) {
        throw new RuntimeException(e.getLocalizedMessage(), e);
    }
}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

/**
 * Converts the JSON to the protobuf message (wrapping exceptions).
 *
 * @param json JSON string representation of protobuf.
 * @param defaultInstance Default instance of protobuf.
 * @param <T> Type of protobuf./*from  w  w  w.java2  s .c  o  m*/
 * @return The deserialized message from the JSON representation.
 * @throws RuntimeException If an error occurs.
 */
@SuppressWarnings({ "unchecked" })
public static <T extends Message> T protobufFromJson(String json, T defaultInstance) throws RuntimeException {
    try {
        return (T) JsonFormat.merge(json, defaultInstance.newBuilderForType());
    } catch (IOException e) {
        throw new RuntimeException(e.getLocalizedMessage(), e);
    }
}

From source file:com.cubusmail.mail.text.MessageTextUtil.java

/**
 * Reads the string out of part's input stream. On first try the input
 * stream retrieved by <code>javax.mail.Part.getInputStream()</code> is
 * used. If an I/O error occurs (<code>java.io.IOException</code>) then the
 * next try is with part's raw input stream. If everything fails an empty
 * string is returned./*from   w  w w  .j av  a2  s.c  o m*/
 * 
 * @param p
 *            - the <code>javax.mail.Part</code> object
 * @param ct
 *            - the part's content type
 * @return the string read from part's input stream or the empty string ""
 *         if everything failed
 * @throws MessagingException
 *             - if an error occurs in part's getter methods
 */
public static String readPart(final Part p) throws MessagingException {

    String contentType = p.getContentType();
    ContentType type = new ContentType(contentType);

    /*
     * Use specified charset if available else use default one
     */
    String charset = type.getParameter("charset");
    if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) {
        charset = CubusConstants.DEFAULT_CHARSET;
    }
    try {
        return readStream(p.getInputStream(), charset);
    } catch (final IOException e) {
        /*
         * Try to get data from raw input stream
         */
        final InputStream inStream;
        if (p instanceof MimeBodyPart) {
            final MimeBodyPart mpb = (MimeBodyPart) p;
            inStream = mpb.getRawInputStream();
        } else if (p instanceof MimeMessage) {
            final MimeMessage mm = (MimeMessage) p;
            inStream = mm.getRawInputStream();
        } else {
            inStream = null;
        }
        if (inStream == null) {
            /*
             * Neither a MimeBodyPart nor a MimeMessage
             */
            return "";
        }
        try {
            return readStream(inStream, charset);
        } catch (final IOException e1) {
            log.error(e1.getLocalizedMessage(), e1);
            return e1.getLocalizedMessage();
            // return STR_EMPTY;
        } finally {
            try {
                inStream.close();
            } catch (final IOException e1) {
                log.error(e1.getLocalizedMessage(), e1);
            }
        }
    }
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Deletes a file. /* ww  w .  j a v  a  2s .co m*/
 * 
 * @param file the path to the file 
 * @return boolean 
 * @throws IOException if an I/O error occurs
 * @throws IllegalArgumentException if file is null
 */
public static boolean deleteFile(Path file) throws IOException {
    if ((file == null))
        throw new IllegalArgumentException("file cannot be null");
    try {
        if (file.toFile().canWrite()) {
            Files.delete(file);
        }
        return !Files.exists(file, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException e) {
        e.printStackTrace();
        throw new IOException("An IO exception occured while deleting file '" + file + "' with error:"
                + e.getLocalizedMessage());
    }
}

From source file:com.hybris.mobile.Hybris.java

private static void populateCategories() {
    String storeName = SDKSettings.getSettingValue(InternalConstants.KEY_PREF_CATALOG);
    try {/* w ww  . j ava 2  s. co m*/
        AssetManager am = context.getAssets();
        if (storeName == null || "".equals(storeName)) {
            storeName = "electronics";
        }
        storeName = "categories." + storeName.substring(0, storeName.indexOf("/")) + ".plist";
        InputStream fs = am.open(storeName);
        CategoryManager.reloadCategories(fs);
    } catch (IOException e) {
        LoggingUtils.e(LOG_TAG, "Error opening file \"" + storeName + "\". " + e.getLocalizedMessage(),
                getAppContext());
    }
}

From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java

public static InputStream connectTo(Context context, URL url, HttpClient client) throws IOException {
    int retries = 1;
    IOException exception;/*  w  ww  .  ja  v a 2 s.  c  om*/
    do {
        try {
            HttpGet request = new HttpGet(url.toString());

            request.setHeader("User-Agent", C.getUserAgentString());

            HttpResponse resp = client.execute(request);
            HttpEntity entity = resp.getEntity();
            InputStream rawContent = entity.getContent();

            return rawContent;
        } catch (IOException e) {
            exception = e;
            Log.e(C.TAG, MessageFormat.format("Problem connecting to {0}. Have {1} retries left", url, retries),
                    e);
        } catch (IllegalStateException e) {
            exception = new IOException(e.getLocalizedMessage());
            Log.e(C.TAG, MessageFormat.format("Problem connecting to {0}. Aborting", url, retries), e);
            break;
        }
    } while (retries-- > 0);
    assert exception != null;
    throw exception;
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

/**
 * /*w w  w. jav a2s.  c  o m*/
 * @param fis
 * @param expectedLength
 *            length to be expected or -2 if you don't want the check
 * @param cipher
 * @return
 */
public static InputStream createInputStream(InputStream fis, long expectedLength, Cipher cipher) {
    try {
        InputStream in = fis;
        ObjectInputStream ois = new ObjectInputStream(in);
        byte marker = readMarker(ois);
        long l = readLength(ois);
        if (log.isDebugEnabled()) {
            log.debug(
                    "JFSEncryptedStream.createInputStream() length check " + expectedLength + " == " + l + "?");
        } // if
        if (expectedLength != DONT_CHECK_LENGTH) {
            if (l != expectedLength) {
                log.error("JFSEncryptedStream.createInputStream() length check failed");
                return null;
            } // if
        } // if
        if (cipher == null) {
            log.error("JFSEncryptedStream.createInputStream() no cipher for length " + expectedLength);
        } else {
            in = new CipherInputStream(in, cipher);
        } // if
        if (marker == COMPRESSION_DEFLATE) {
            Inflater inflater = new Inflater(true);
            in = new InflaterInputStream(in, inflater, COMPRESSION_BUFFER_SIZE);
        } // if
        if (marker == COMPRESSION_BZIP2) {
            in = new BZip2CompressorInputStream(in);
        } // if
        if (marker == COMPRESSION_LZMA) {
            Decoder decoder = new Decoder();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            byte[] properties = new byte[5];
            int readBytes = in.read(properties, 0, properties.length);
            boolean result = decoder.SetDecoderProperties(properties);
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() readBytes=" + readBytes);
                log.debug("JFSEncryptedStream.createInputStream() result=" + result);
            } // if

            decoder.Code(in, outputStream, l);
            in.close();
            outputStream.close();
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() " + outputStream.size());
            } // if
            in = new ByteArrayInputStream(outputStream.toByteArray());
        } // if
        return in;
    } catch (IOException ioe) {
        log.error("JFSEncryptedStream.createInputStream() I/O Exception " + ioe.getLocalizedMessage());
        return null;
    } // try/catch
}