alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (Image.java)

This example Jetty source code file (Image.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

file, fileinputstream, fileinputstream, image, image, io, ioexception, ioexception, string, string, tag

The Jetty Image.java source code

// ========================================================================
// $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at 
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================

package org.mortbay.html;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.mortbay.log.Log;
import org.mortbay.util.IO;


/* ---------------------------------------------------------------- */
/** HTML Image Tag.
 * @see org.mortbay.html.Block
 * @version $Id: Image.java,v 1.8 2005/08/13 00:01:23 gregwilkins Exp $
 * @author Greg Wilkins
*/
public class Image extends Tag
{

    /* ------------------------------------------------------------ */
    public Image(String src)
    {
        super("img");
        attribute("src",src);
    }
    
    /* ------------------------------------------------------------ */
    /** Construct from GIF file.
     */
    public Image(String dirname, String src)
    {
        super("img");
        attribute("src",src);
        setSizeFromGif(dirname,src);
    }
    
    /* ------------------------------------------------------------ */
    /** Construct from GIF file.
     */
    public Image(File gif)
    {
        super("img");
        attribute("src",gif.getName());
        setSizeFromGif(gif);
    }

    /* ------------------------------------------------------------ */
    public Image(String src,int width, int height, int border)
    {
        this(src);
        width(width);
        height(height);
        border(border);
    }
    
    /* ------------------------------------------------------------ */
    public Image border(int b)
    {
        attribute("border",b);
        return this;
    }
    
    /* ------------------------------------------------------------ */
    public Image alt(String alt)
    {
        attribute("alt",alt);
        return this;
    }
    
    /* ------------------------------------------------------------ */
    /** Set the image size from the header of a GIF file.
     * @param dirname The directory name, expected to be in OS format
     * @param pathname The image path name relative to the directory.
     *                 Expected to be in WWW format (i.e. with slashes)
     *                 and will be converted to OS format.
     */
    public Image setSizeFromGif(String dirname,
                                String pathname)
    {
        String filename =dirname + pathname.replace('/',File.separatorChar);
        return setSizeFromGif(filename);
    }
    
    /* ------------------------------------------------------------ */
    /** Set the image size from the header of a GIF file.
     */
    public Image setSizeFromGif(String filename)
    {
        return setSizeFromGif(new File(filename));
    }
    
    /* ------------------------------------------------------------ */
    /** Set the image size from the header of a GIF file.
     */
    public Image setSizeFromGif(File gif)
    {
        if (gif.canRead())
        {
            FileInputStream in = null;
            try{
                byte [] buf = new byte[10];
                in = new FileInputStream(gif);
                if (in.read(buf,0,10)==10)
                {
                    if(Log.isDebugEnabled())Log.debug("Image "+gif.getName()+
                               " is " +
                               ((0x00ff&buf[7])*256+(0x00ff&buf[6])) +
                               " x " +
                               (((0x00ff&buf[9])*256+(0x00ff&buf[8]))));
                    width((0x00ff&buf[7])*256+(0x00ff&buf[6]));
                    height(((0x00ff&buf[9])*256+(0x00ff&buf[8])));
                }
            }
            catch (IOException e){
                Log.ignore(e);
            }
            finally {
                IO.close(in);
            }
        }
        
        return this;
    }
    
}



Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty Image.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.