View Javadoc

1   /*
2   
3       dsh-piccolo-tilemap  Piccolo2D tile map nodes and supporting classes.
4       Copyright (c) 2006-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.piccolo.tilemap.io.impl;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  import java.net.URL;
32  
33  import org.dishevelled.piccolo.tilemap.AbstractTileMap;
34  
35  import org.dishevelled.piccolo.tilemap.io.TileMapReader;
36  
37  /**
38   * Abstract tile map reader.
39   *
40   * @author  Michael Heuer
41   * @version $Revision$ $Date$
42   */
43  public abstract class AbstractTileMapReader
44      implements TileMapReader
45  {
46  
47      @Override
48      public final AbstractTileMap read(final File file) throws IOException
49      {
50          if (file == null)
51          {
52              throw new IllegalArgumentException("file must not be null");
53          }
54          InputStream inputStream = null;
55          try
56          {
57              inputStream = new FileInputStream(file);
58              return read(inputStream);
59          }
60          catch (IOException e)
61          {
62              throw e;
63          }
64          finally
65          {
66              closeQuietly(inputStream);
67          }
68      }
69  
70      @Override
71      public final AbstractTileMap read(final URL url) throws IOException
72      {
73          if (url == null)
74          {
75              throw new IllegalArgumentException("url must not be null");
76          }
77          InputStream inputStream = null;
78          try
79          {
80              inputStream = url.openStream();
81              return read(inputStream);
82          }
83          catch (IOException e)
84          {
85              throw e;
86          }
87          finally
88          {
89              closeQuietly(inputStream);
90          }
91      }
92  
93      /**
94       * Close the specified input stream quietly.
95       *
96       * @param inputStream input stream to close
97       */
98      protected static final void closeQuietly(final InputStream inputStream)
99      {
100         try
101         {
102             inputStream.close();
103         }
104         catch (Exception e)
105         {
106             // ignore
107         }
108     }
109 }