bit.changepurse.wdk.util.CheckedExceptionMethods.java Source code

Java tutorial

Introduction

Here is the source code for bit.changepurse.wdk.util.CheckedExceptionMethods.java

Source

/*
 * This file is part of ChangePurse.
 *
 * Copyright (c) 2014 Germn Fuentes Capella
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package bit.changepurse.wdk.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.zip.BitInputStream;

/**
 *
 * No checked exceptions in most of the code. They might be good in some cases,
 * but not in every situation.
 *
 */
public class CheckedExceptionMethods {
    private CheckedExceptionMethods() {
        throw new AssertionError("No bit.coinage.wdk.utils.CheckedExceptionMethods instances for you!");
    }

    public static URL newURL(String url) {
        try {
            return new URL(url);
        } catch (MalformedURLException e) {
            throw new UncheckedException(e);
        }
    }

    public static URI newURI(URL url) {
        try {
            return url.toURI();
        } catch (URISyntaxException | NullPointerException e) {
            throw new UncheckedException(e);
        }
    }

    public static URI newURI(String url) {
        return newURI(newURL(url));
    }

    public static HttpEntity newHttpEntity(List<? extends NameValuePair> params) {
        try {
            return new UrlEncodedFormEntity(params);
        } catch (UnsupportedEncodingException e) {
            throw new UncheckedException(e);
        }
    }

    public static HttpEntity newHttpEntity(String payload) {
        try {
            return new StringEntity(payload);
        } catch (UnsupportedEncodingException e) {
            throw new UncheckedException(e);
        }
    }

    public static CloseableHttpResponse execute(CloseableHttpClient client, HttpRequestBase request) {
        try {
            return client.execute(request);
        } catch (IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static String toString(HttpEntity entity) {
        try {
            return entity != null ? EntityUtils.toString(entity) : "";
        } catch (ParseException | IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static HttpEntity toHttpEntity(String entity) {
        try {
            return new StringEntity(entity != null ? entity : "");
        } catch (UnsupportedEncodingException e) {
            throw new UncheckedException(e);
        }
    }

    public static MessageDigest getMessageDigest(HashAlgorithm alg) {
        try {
            return MessageDigest.getInstance(alg.toString());
        } catch (NoSuchAlgorithmException e) {
            throw new UncheckedException(e);
        }
    }

    public static void write(OutputStream os, byte[] bytes) {
        try {
            os.write(bytes);
        } catch (IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static List<String> readAllLines(Path path) {
        try {
            return Files.readAllLines(path);
        } catch (IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static String readTextFile(Path path) {
        try {
            return new String(Files.readAllBytes(path));
        } catch (IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static int readBits(BitInputStream is, int numBits) {
        try {
            return is.read(numBits);
        } catch (IOException e) {
            throw new UncheckedException(e);
        }
    }

    public static SecretKeyFactory getSecretKeyFactory(KeyDerivationAlgorithm algorithm) {
        try {
            return SecretKeyFactory.getInstance(algorithm.toString());
        } catch (NoSuchAlgorithmException e) {
            throw new UncheckedException(e);
        }
    }

    public static byte[] generateSecret(SecretKeyFactory skf, PBEKeySpec spec) {
        try {
            return skf.generateSecret(spec).getEncoded();
        } catch (InvalidKeySpecException e) {
            throw new UncheckedException(e);
        }
    }
}