001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.request.resource;
018
019import org.apache.wicket.request.Response;
020
021/**
022 * An {@link IResource} for byte arrays. The byte array can be static - passed to the constructor,
023 * or dynamic - by overriding
024 * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
025 * 
026 * @author Matej Knopp
027 */
028public class ByteArrayResource extends BaseDataResource<byte[]>
029{
030        private static final long serialVersionUID = 1L;
031
032        /**
033         * Creates a {@link ByteArrayResource} which will provide its data dynamically with
034         * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
035         * 
036         * @param contentType
037         *            The Content type of the array.
038         */
039        public ByteArrayResource(final String contentType)
040        {
041                super(contentType);
042        }
043
044        /**
045         * Creates a Resource from the given byte array with its content type
046         * 
047         * @param contentType
048         *            The Content type of the array.
049         * @param array
050         *            The binary content
051         */
052        public ByteArrayResource(final String contentType, final byte[] array)
053        {
054                super(contentType, array);
055        }
056
057        /**
058         * Creates a Resource from the given byte array with its content type
059         * 
060         * @param contentType
061         *            The Content type of the array.
062         * @param array
063         *            The binary content
064         * @param filename
065         *            The filename that will be set as the Content-Disposition header.
066         */
067        public ByteArrayResource(final String contentType, final byte[] array, final String filename)
068        {
069                super(contentType, array, filename);
070        }
071
072        @Override
073        protected void writeData(Response response, byte[] data)
074        {
075                response.write(data);
076        }
077
078        @Override
079        protected Long getLength(byte[] data)
080        {
081                return (long) data.length;
082        }
083}