Java URL Connection map2URL(final Properties properties)

Here you can find the source of map2URL(final Properties properties)

Description

map URL

License

Open Source License

Declaration

private static URL map2URL(final Properties properties) 

Method Source Code


//package com.java2s;
/*/*ww w.  ja v a  2s.c o m*/
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 * Portions Copyrighted 2014 ForgeRock AS.
 */

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import java.util.Map;

import java.util.Properties;

public class Main {
    private static URL map2URL(final Properties properties) {
        URLStreamHandler handler = new URLStreamHandler() {
            @Override
            protected URLConnection openConnection(URL u) throws IOException {
                return new URLConnection(u) {
                    @Override
                    public void connect() throws IOException {
                    }

                    @Override
                    public InputStream getInputStream() throws IOException {
                        // Here we store properties to stream, we cannot use
                        // Properties.store(), because
                        // this method adds comment # character which would not
                        // be parsable by Groovy
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(baos));
                        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                            bfw.append(entry.getKey().toString());
                            bfw.append("=");
                            bfw.append(entry.getValue().toString());
                            bfw.newLine();
                        }
                        bfw.close();
                        return new ByteArrayInputStream(baos.toByteArray());
                    }
                };
            }

        };
        try {
            return new URL(null, "file:///map", handler);
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Invalid url", e);
        }
    }
}

Related

  1. lastModifiedURL(String urlstr)
  2. loadCookie(URL url)
  3. loadJar(String path, URL resource)
  4. loadLinesUrl(String url)
  5. loadObjectFromURL(URL url)
  6. parseURL(ClassLoader classLoader, String uri)
  7. pingUrl(URL url)
  8. queryComputeAPI(String url)
  9. renderRequest(URLConnection connection)