001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.devtools.restart.classloader;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.URL;
023import java.net.URLConnection;
024import java.net.URLStreamHandler;
025
026/**
027 * {@link URLStreamHandler} for the contents of a {@link ClassLoaderFile}.
028 *
029 * @author Phillip Webb
030 * @since 1.5.0
031 */
032public class ClassLoaderFileURLStreamHandler extends URLStreamHandler {
033
034        private ClassLoaderFile file;
035
036        public ClassLoaderFileURLStreamHandler(ClassLoaderFile file) {
037                this.file = file;
038        }
039
040        @Override
041        protected URLConnection openConnection(URL url) throws IOException {
042                return new Connection(url);
043        }
044
045        private class Connection extends URLConnection {
046
047                Connection(URL url) {
048                        super(url);
049                }
050
051                @Override
052                public void connect() throws IOException {
053                }
054
055                @Override
056                public InputStream getInputStream() throws IOException {
057                        return new ByteArrayInputStream(
058                                        ClassLoaderFileURLStreamHandler.this.file.getContents());
059                }
060
061                @Override
062                public long getLastModified() {
063                        return ClassLoaderFileURLStreamHandler.this.file.getLastModified();
064
065                }
066
067        }
068
069}