2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.FeatureSettingsModelI;
27 import jalview.bin.Cache;
28 import jalview.util.MessageManager;
30 import java.io.BufferedInputStream;
31 import java.io.BufferedReader;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.Reader;
38 import java.io.StringReader;
39 import java.net.HttpURLConnection;
40 import java.net.MalformedURLException;
42 import java.net.URLConnection;
43 import java.util.zip.GZIPInputStream;
47 * implements a random access wrapper around a particular datasource, for
48 * passing to identifyFile and AlignFile objects.
50 public class FileParse
52 protected static final String SPACE = " ";
54 protected static final String TAB = "\t";
57 * text specifying source of data. usually filename or url.
59 private String dataName = "unknown source";
61 public File inFile = null;
65 * a viewport associated with the current file operation. May be null. May
66 * move to different object.
68 private AlignViewportI viewport;
71 * specific settings for exporting data from the current context
73 private AlignExportSettingI exportSettings;
76 * sequence counter for FileParse object created from same data source
81 * separator for extracting specific 'frame' of a datasource for formats that
82 * support multiple records (e.g. BLC, Stockholm, etc)
84 protected char suffixSeparator = '#';
87 * character used to write newlines
89 protected String newline = System.getProperty("line.separator");
91 public void setNewlineString(String nl)
96 public String getNewlineString()
102 * '#' separated string tagged on to end of filename or url that was clipped
103 * off to resolve to valid filename
105 protected String suffix = null;
107 protected DataSourceType dataSourceType = null;
109 protected BufferedReader dataIn = null;
111 protected String errormessage = "UNINITIALISED SOURCE";
113 protected boolean error = true;
115 protected String warningMessage = null;
118 * size of readahead buffer used for when initial stream position is marked.
120 final int READAHEAD_LIMIT = 2048;
127 * Create a new FileParse instance reading from the same datasource starting
128 * at the current position. WARNING! Subsequent reads from either object will
129 * affect the read position of the other, but not the error state.
133 public FileParse(FileParse from) throws IOException
137 throw new Error(MessageManager
138 .getString("error.implementation_error_null_fileparse"));
144 index = ++from.index;
145 inFile = from.inFile;
146 suffixSeparator = from.suffixSeparator;
147 suffix = from.suffix;
148 errormessage = from.errormessage; // inherit potential error messages
149 error = false; // reset any error condition.
150 dataSourceType = from.dataSourceType;
151 dataIn = from.dataIn;
156 dataName = from.dataName;
160 * Attempt to open a file as a datasource. Sets error and errormessage if
161 * fileStr was invalid.
164 * @return this.error (true if the source was invalid)
166 private boolean checkFileSource(String fileStr) throws IOException
169 this.inFile = new File(fileStr);
170 // check to see if it's a Jar file in disguise.
171 if (!inFile.exists())
173 errormessage = "FILE NOT FOUND";
176 if (!inFile.canRead())
178 errormessage = "FILE CANNOT BE OPENED FOR READING";
181 if (inFile.isDirectory())
183 // this is really a 'complex' filetype - but we don't handle directory
185 errormessage = "FILE IS A DIRECTORY";
192 dataIn = checkForGzipStream(new FileInputStream(fileStr));
194 } catch (Exception x)
196 warningMessage = "Failed to resolve " + fileStr
197 + " as a data source. (" + x.getMessage() + ")";
198 // x.printStackTrace();
207 * Recognise the 2-byte magic header indicating a gzipped stream
210 * https://recalll.co/ask/v/topic/java-How-to-check-if-InputStream-is-Gzipped/555aadd62bd27354438b90f6
213 * - input stream that supports mark and contains at least two bytes
215 * @return false if mark not supported or no magic header found
217 * @throws IOException
219 public static boolean isGzipStream(InputStream input) throws IOException
221 if (!input.markSupported())
224 "FileParse.izGzipStream: input stream must support mark/reset");
229 // get first 2 bytes or return false
230 byte[] bytes = new byte[2];
231 int read = input.read(bytes);
233 if (read != bytes.length)
238 int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
239 return (GZIPInputStream.GZIP_MAGIC == header);
243 * Returns a Reader for the given input after wrapping it in a buffered input
244 * stream, and then checking if it needs to be wrapped by a GZipInputStream
249 private BufferedReader checkForGzipStream(InputStream input) throws Exception {
251 // NB: stackoverflow https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
252 // could use a PushBackInputStream rather than a BufferedInputStream
254 if (!input.markSupported()) {
255 input = new BufferedInputStream(input,16);
257 if (isGzipStream(input)) {
258 return getGzipReader(input);
260 // return a buffered reader for the stream.
261 InputStreamReader isReader= new InputStreamReader(input);
262 BufferedReader toReadFrom=new BufferedReader(isReader);
266 * Returns a {@code BufferedReader} which wraps the input stream with a
267 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
268 * occurs or the compression method used is unsupported.
274 private BufferedReader getGzipReader(InputStream inputStream)
277 BufferedReader inData = new BufferedReader(
278 new InputStreamReader(new GZIPInputStream(inputStream)));
286 * Tries to read from the given URL. If successful, saves a reader to the
287 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
288 * status not 200), throws an exception.
290 * If the response status includes
293 * Content-Type : application/x-gzip
296 * then tries to read as gzipped content.
299 * @throws IOException
300 * @throws MalformedURLException
302 private void checkURLSource(String urlStr)
303 throws IOException, MalformedURLException
305 errormessage = "URL NOT FOUND";
306 URL url = new URL(urlStr);
307 URLConnection _conn = url.openConnection();
308 if (_conn instanceof HttpURLConnection)
310 HttpURLConnection conn = (HttpURLConnection) _conn;
311 int rc = conn.getResponseCode();
312 if (rc != HttpURLConnection.HTTP_OK)
314 throw new IOException(
315 "Response status from " + urlStr + " was " + rc);
319 dataIn = checkForGzipStream(_conn.getInputStream());
321 } catch (IOException ex)
323 throw new IOException("Failed to handle non-HTTP URI stream",ex);
324 } catch (Exception ex)
326 throw new IOException("Failed to determine type of input stream for given URI",ex);
330 String encoding = _conn.getContentEncoding();
331 String contentType = _conn.getContentType();
332 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
333 || "gzip".equals(encoding);
335 InputStream inputStream = _conn.getInputStream();
340 dataIn = getGzipReader(inputStream);
342 } catch (Exception e1)
344 throw new IOException(MessageManager
345 .getString("exception.failed_to_resolve_gzip_stream"), e);
350 dataIn = new BufferedReader(new InputStreamReader(inputStream));
356 * sets the suffix string (if any) and returns remainder (if suffix was
360 * @return truncated fileStr or null
362 private String extractSuffix(String fileStr)
364 // first check that there wasn't a suffix string tagged on.
365 int sfpos = fileStr.lastIndexOf(suffixSeparator);
366 if (sfpos > -1 && sfpos < fileStr.length() - 1)
368 suffix = fileStr.substring(sfpos + 1);
369 // System.err.println("DEBUG: Found Suffix:"+suffix);
370 return fileStr.substring(0, sfpos);
376 * not for general use, creates a fileParse object for an existing reader with
377 * configurable values for the origin and the type of the source
379 public FileParse(BufferedReader source, String originString,
380 DataSourceType sourceType)
382 dataSourceType = sourceType;
385 dataName = originString;
389 if (dataIn.markSupported())
391 dataIn.mark(READAHEAD_LIMIT);
393 } catch (IOException q)
400 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
401 * types of sources that are handled.
404 * - datasource locator/content
406 * - protocol of source
407 * @throws MalformedURLException
408 * @throws IOException
410 public FileParse(String fileStr, DataSourceType sourceType)
411 throws MalformedURLException, IOException
413 this.dataSourceType = sourceType;
416 if (sourceType == DataSourceType.FILE)
418 if (checkFileSource(fileStr))
420 String suffixLess = extractSuffix(fileStr);
421 if (suffixLess != null)
423 if (checkFileSource(suffixLess))
425 throw new IOException(MessageManager.formatMessage(
426 "exception.problem_opening_file_also_tried",
428 { inFile.getName(), suffixLess, errormessage }));
433 throw new IOException(MessageManager.formatMessage(
434 "exception.problem_opening_file", new String[]
435 { inFile.getName(), errormessage }));
439 else if (sourceType == DataSourceType.URL)
445 checkURLSource(fileStr);
446 if (suffixSeparator == '#')
448 extractSuffix(fileStr); // URL lref is stored for later reference.
450 } catch (IOException e)
452 String suffixLess = extractSuffix(fileStr);
453 if (suffixLess == null)
461 checkURLSource(suffixLess);
462 } catch (IOException e2)
464 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
465 throw (e); // just pass back original - everything was wrong.
469 } catch (Exception e)
471 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
472 + e.getMessage() + ")";
476 else if (sourceType == DataSourceType.PASTE)
478 errormessage = "PASTE INACCESSIBLE!";
479 dataIn = new BufferedReader(new StringReader(fileStr));
482 else if (sourceType == DataSourceType.CLASSLOADER)
484 errormessage = "RESOURCE CANNOT BE LOCATED";
485 java.io.InputStream is = getClass()
486 .getResourceAsStream("/" + fileStr);
489 String suffixLess = extractSuffix(fileStr);
490 if (suffixLess != null)
492 is = getClass().getResourceAsStream("/" + suffixLess);
497 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
507 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
508 + (sourceType != null ? sourceType : "null") + "'";
511 if (dataIn == null || error)
513 // pass up the reason why we have no source to read from
514 throw new IOException(MessageManager.formatMessage(
515 "exception.failed_to_read_data_from_source", new String[]
519 dataIn.mark(READAHEAD_LIMIT);
523 * mark the current position in the source as start for the purposes of it
524 * being analysed by IdentifyFile().identify
526 * @throws IOException
528 public void mark() throws IOException
532 dataIn.mark(READAHEAD_LIMIT);
536 throw new IOException(
537 MessageManager.getString("exception.no_init_source_stream"));
541 public String nextLine() throws IOException
545 return dataIn.readLine();
547 throw new IOException(MessageManager
548 .formatMessage("exception.invalid_source_stream", new String[]
554 * @return true if this FileParse is configured for Export only
556 public boolean isExporting()
558 return !error && dataIn == null;
563 * @return true if the data source is valid
565 public boolean isValid()
571 * closes the datasource and tidies up. source will be left in an error state
573 public void close() throws IOException
575 errormessage = "EXCEPTION ON CLOSE";
579 errormessage = "SOURCE IS CLOSED";
583 * Rewinds the datasource to the marked point if possible
588 public void reset(int bytesRead) throws IOException
590 if (bytesRead >= READAHEAD_LIMIT)
592 System.err.println(String.format(
593 "File reset error: read %d bytes but reset limit is %d",
594 bytesRead, READAHEAD_LIMIT));
596 if (dataIn != null && !error)
602 throw new IOException(MessageManager.getString(
603 "error.implementation_error_reset_called_for_invalid_source"));
609 * @return true if there is a warning for the user
611 public boolean hasWarningMessage()
613 return (warningMessage != null && warningMessage.length() > 0);
618 * @return empty string or warning message about file that was just parsed.
620 public String getWarningMessage()
622 return warningMessage;
625 public String getInFile()
629 return inFile.getAbsolutePath() + " (" + index + ")";
633 return "From Paste + (" + index + ")";
638 * @return the dataName
640 public String getDataName()
646 * set the (human readable) name or URI for this datasource
650 protected void setDataName(String dataname)
656 * get the underlying bufferedReader for this data source.
658 * @return null if no reader available
659 * @throws IOException
661 public Reader getReader()
663 if (dataIn != null) // Probably don't need to test for readiness &&
671 public AlignViewportI getViewport()
676 public void setViewport(AlignViewportI viewport)
678 this.viewport = viewport;
682 * @return the currently configured exportSettings for writing data.
684 public AlignExportSettingI getExportSettings()
686 return exportSettings;
690 * Set configuration for export of data.
692 * @param exportSettings
693 * the exportSettings to set
695 public void setExportSettings(AlignExportSettingI exportSettings)
697 this.exportSettings = exportSettings;
701 * method overridden by complex file exporter/importers which support
702 * exporting visualisation and layout settings for a view
706 public void configureForView(AlignmentViewPanel avpanel)
710 setViewport(avpanel.getAlignViewport());
712 // could also set export/import settings
716 * Returns the preferred feature colour configuration if there is one, else
721 public FeatureSettingsModelI getFeatureColourScheme()
726 public DataSourceType getDataSourceType()
728 return dataSourceType;