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.AlignExportSettingsI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureSettingsModelI;
26 import jalview.util.MessageManager;
28 import java.io.BufferedInputStream;
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
32 import java.io.FileInputStream;
33 import java.io.FileReader;
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;
45 import jalview.util.Platform;
48 * implements a random access wrapper around a particular datasource, for
49 * passing to identifyFile and AlignFile objects.
51 public class FileParse
53 protected static final String SPACE = " ";
55 protected static final String TAB = "\t";
58 * text specifying source of data. usually filename or url.
60 private String dataName = "unknown source";
62 public File inFile = null;
64 private byte[] bytes; // from JavaScript
66 public byte[] getBytes()
72 * a viewport associated with the current file operation. May be null. May
73 * move to different object.
75 private AlignViewportI viewport;
78 * specific settings for exporting data from the current context
80 private AlignExportSettingsI exportSettings;
83 * sequence counter for FileParse object created from same data source
88 * separator for extracting specific 'frame' of a datasource for formats that
89 * support multiple records (e.g. BLC, Stockholm, etc)
91 protected char suffixSeparator = '#';
94 * character used to write newlines
96 protected String newline = System.getProperty("line.separator");
98 public void setNewlineString(String nl)
103 public String getNewlineString()
109 * '#' separated string tagged on to end of filename or url that was clipped
110 * off to resolve to valid filename
112 protected String suffix = null;
114 protected DataSourceType dataSourceType = null;
116 protected BufferedReader dataIn = null;
118 protected String errormessage = "UNINITIALISED SOURCE";
120 protected boolean error = true;
122 protected String warningMessage = null;
125 * size of readahead buffer used for when initial stream position is marked.
127 final int READAHEAD_LIMIT = 2048;
134 * Create a new FileParse instance reading from the same datasource starting
135 * at the current position. WARNING! Subsequent reads from either object will
136 * affect the read position of the other, but not the error state.
140 public FileParse(FileParse from) throws IOException
144 throw new Error(MessageManager
145 .getString("error.implementation_error_null_fileparse"));
151 index = ++from.index;
152 inFile = from.inFile;
153 suffixSeparator = from.suffixSeparator;
154 suffix = from.suffix;
155 errormessage = from.errormessage; // inherit potential error messages
156 error = false; // reset any error condition.
157 dataSourceType = from.dataSourceType;
158 dataIn = from.dataIn;
163 dataName = from.dataName;
167 * Attempt to open a file as a datasource. Sets error and errormessage if
168 * fileStr was invalid.
171 * @return this.error (true if the source was invalid)
173 private boolean checkFileSource(String fileStr) throws IOException
176 this.inFile = new File(fileStr);
177 // check to see if it's a Jar file in disguise.
178 if (!inFile.exists())
180 errormessage = "FILE NOT FOUND";
183 if (!inFile.canRead())
185 errormessage = "FILE CANNOT BE OPENED FOR READING";
188 if (inFile.isDirectory())
190 // this is really a 'complex' filetype - but we don't handle directory
192 errormessage = "FILE IS A DIRECTORY";
199 dataIn = checkForGzipStream(new FileInputStream(fileStr));
201 } catch (Exception x)
203 warningMessage = "Failed to resolve " + fileStr
204 + " as a data source. (" + x.getMessage() + ")";
205 // x.printStackTrace();
214 * Recognise the 2-byte magic header for gzip streams
216 * https://recalll.co/ask/v/topic/java-How-to-check-if-InputStream-is-Gzipped/555aadd62bd27354438b90f6
218 * @param bytes - at least two bytes
221 private static boolean isGzipStream(byte[] bytes) {
222 int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
223 return (GZIPInputStream.GZIP_MAGIC == head);
227 * Returns a Reader for the given input after wrapping it in a buffered input
228 * stream, and then checking if it needs to be wrapped by a GZipInputStream
233 private BufferedReader checkForGzipStream(InputStream input) throws Exception {
235 // NB: stackoverflow https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
236 // could use a PushBackInputStream rather than a BufferedInputStream
238 BufferedInputStream bufinput;
239 if (!input.markSupported()) {
240 bufinput= new BufferedInputStream(input,16);
244 byte[] bytes=input.readNBytes(2);
246 if (bytes.length==2 && isGzipStream(bytes)) {
247 return getGzipReader(input);
249 // return a buffered reader for the stream.
250 InputStreamReader isReader= new InputStreamReader(input);
251 BufferedReader toReadFrom=new BufferedReader(isReader);
255 * Returns a {@code BufferedReader} which wraps the input stream with a
256 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
257 * occurs or the compression method used is unsupported.
263 private BufferedReader getGzipReader(InputStream inputStream)
266 BufferedReader inData = new BufferedReader(
267 new InputStreamReader(new GZIPInputStream(inputStream)));
275 * Tries to read from the given URL. If successful, saves a reader to the
276 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
277 * status not 200), throws an exception.
279 * If the response status includes
282 * Content-Type : application/x-gzip
285 * then tries to read as gzipped content.
288 * @throws IOException
289 * @throws MalformedURLException
291 private void checkURLSource(String urlStr)
292 throws IOException, MalformedURLException
294 errormessage = "URL NOT FOUND";
295 URL url = new URL(urlStr);
296 URLConnection _conn = url.openConnection();
297 if (_conn instanceof HttpURLConnection)
299 HttpURLConnection conn = (HttpURLConnection) _conn;
300 int rc = conn.getResponseCode();
301 if (rc != HttpURLConnection.HTTP_OK)
303 throw new IOException(
304 "Response status from " + urlStr + " was " + rc);
308 dataIn = checkForGzipStream(_conn.getInputStream());
310 } catch (IOException ex)
312 throw new IOException("Failed to handle non-HTTP URI stream",ex);
313 } catch (Exception ex)
315 throw new IOException("Failed to determine type of input stream for given URI",ex);
319 String encoding = _conn.getContentEncoding();
320 String contentType = _conn.getContentType();
321 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
322 || "gzip".equals(encoding);
324 InputStream inputStream = _conn.getInputStream();
329 dataIn = getGzipReader(inputStream);
331 } catch (Exception e1)
333 throw new IOException(MessageManager
334 .getString("exception.failed_to_resolve_gzip_stream"), e);
339 dataIn = new BufferedReader(new InputStreamReader(inputStream));
345 * sets the suffix string (if any) and returns remainder (if suffix was
349 * @return truncated fileStr or null
351 private String extractSuffix(String fileStr)
353 // first check that there wasn't a suffix string tagged on.
354 int sfpos = fileStr.lastIndexOf(suffixSeparator);
355 if (sfpos > -1 && sfpos < fileStr.length() - 1)
357 suffix = fileStr.substring(sfpos + 1);
358 // System.err.println("DEBUG: Found Suffix:"+suffix);
359 return fileStr.substring(0, sfpos);
365 * not for general use, creates a fileParse object for an existing reader with
366 * configurable values for the origin and the type of the source
368 public FileParse(BufferedReader source, String originString,
369 DataSourceType sourceType)
371 dataSourceType = sourceType;
374 dataName = originString;
378 if (dataIn.markSupported())
380 dataIn.mark(READAHEAD_LIMIT);
382 } catch (IOException q)
389 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
390 * types of sources that are handled.
393 * - datasource locator/content as File or String
395 * - protocol of source
396 * @throws MalformedURLException
397 * @throws IOException
399 public FileParse(Object file, DataSourceType sourceType)
400 throws MalformedURLException, IOException
402 if (file instanceof File)
404 parse((File) file, ((File) file).getPath(), sourceType, true);
408 parse(null, file.toString(), sourceType, false);
412 private void parse(File file, String fileStr, DataSourceType sourceType,
413 boolean isFileObject) throws IOException
415 bytes = Platform.getFileBytes(file);
416 dataSourceType = sourceType;
419 if (sourceType == DataSourceType.FILE)
424 // this will be from JavaScript
426 dataIn = new BufferedReader(
427 new InputStreamReader(new ByteArrayInputStream(bytes)));
430 else if (checkFileSource(fileStr))
432 String suffixLess = extractSuffix(fileStr);
433 if (suffixLess != null)
435 if (checkFileSource(suffixLess))
437 throw new IOException(MessageManager.formatMessage(
438 "exception.problem_opening_file_also_tried",
440 { inFile.getName(), suffixLess, errormessage }));
445 throw new IOException(MessageManager.formatMessage(
446 "exception.problem_opening_file", new String[]
447 { inFile.getName(), errormessage }));
451 else if (sourceType == DataSourceType.RELATIVE_URL)
453 // BH 2018 hack for no support for access-origin
454 bytes = Platform.getFileAsBytes(fileStr);
455 dataIn = new BufferedReader(
456 new InputStreamReader(new ByteArrayInputStream(bytes)));
460 else if (sourceType == DataSourceType.URL)
466 checkURLSource(fileStr);
467 if (suffixSeparator == '#')
469 extractSuffix(fileStr); // URL lref is stored for later reference.
471 } catch (IOException e)
473 String suffixLess = extractSuffix(fileStr);
474 if (suffixLess == null)
482 checkURLSource(suffixLess);
483 } catch (IOException e2)
485 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
486 throw (e); // just pass back original - everything was wrong.
490 } catch (Exception e)
492 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
493 + e.getMessage() + ")";
497 else if (sourceType == DataSourceType.PASTE)
499 errormessage = "PASTE INACCESSIBLE!";
500 dataIn = new BufferedReader(new StringReader(fileStr));
503 else if (sourceType == DataSourceType.CLASSLOADER)
505 errormessage = "RESOURCE CANNOT BE LOCATED";
506 InputStream is = getClass().getResourceAsStream("/" + fileStr);
509 String suffixLess = extractSuffix(fileStr);
510 if (suffixLess != null)
512 is = getClass().getResourceAsStream("/" + suffixLess);
517 dataIn = new BufferedReader(new InputStreamReader(is));
527 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
528 + (sourceType != null ? sourceType : "null") + "'";
531 if (dataIn == null || error)
533 // pass up the reason why we have no source to read from
534 throw new IOException(MessageManager.formatMessage(
535 "exception.failed_to_read_data_from_source", new String[]
539 dataIn.mark(READAHEAD_LIMIT);
543 * mark the current position in the source as start for the purposes of it
544 * being analysed by IdentifyFile().identify
546 * @throws IOException
548 public void mark() throws IOException
552 dataIn.mark(READAHEAD_LIMIT);
556 throw new IOException(
557 MessageManager.getString("exception.no_init_source_stream"));
561 public String nextLine() throws IOException
565 return dataIn.readLine();
567 throw new IOException(MessageManager
568 .formatMessage("exception.invalid_source_stream", new String[]
574 * @return true if this FileParse is configured for Export only
576 public boolean isExporting()
578 return !error && dataIn == null;
583 * @return true if the data source is valid
585 public boolean isValid()
591 * closes the datasource and tidies up. source will be left in an error state
593 public void close() throws IOException
595 errormessage = "EXCEPTION ON CLOSE";
599 errormessage = "SOURCE IS CLOSED";
603 * Rewinds the datasource to the marked point if possible
608 public void reset(int bytesRead) throws IOException
610 if (bytesRead >= READAHEAD_LIMIT)
612 System.err.println(String.format(
613 "File reset error: read %d bytes but reset limit is %d",
614 bytesRead, READAHEAD_LIMIT));
616 if (dataIn != null && !error)
622 throw new IOException(MessageManager.getString(
623 "error.implementation_error_reset_called_for_invalid_source"));
629 * @return true if there is a warning for the user
631 public boolean hasWarningMessage()
633 return (warningMessage != null && warningMessage.length() > 0);
638 * @return empty string or warning message about file that was just parsed.
640 public String getWarningMessage()
642 return warningMessage;
645 public String getInFile()
649 return inFile.getAbsolutePath() + " (" + index + ")";
653 return "From Paste + (" + index + ")";
658 * @return the dataName
660 public String getDataName()
666 * set the (human readable) name or URI for this datasource
670 protected void setDataName(String dataname)
676 * get the underlying bufferedReader for this data source.
678 * @return null if no reader available
679 * @throws IOException
681 public Reader getReader()
683 if (dataIn != null) // Probably don't need to test for readiness &&
691 public AlignViewportI getViewport()
696 public void setViewport(AlignViewportI viewport)
698 this.viewport = viewport;
702 * @return the currently configured exportSettings for writing data.
704 public AlignExportSettingsI getExportSettings()
706 return exportSettings;
710 * Set configuration for export of data.
712 * @param exportSettings
713 * the exportSettings to set
715 public void setExportSettings(AlignExportSettingsI exportSettings)
717 this.exportSettings = exportSettings;
721 * method overridden by complex file exporter/importers which support
722 * exporting visualisation and layout settings for a view
726 public void configureForView(AlignmentViewPanel avpanel)
730 setViewport(avpanel.getAlignViewport());
732 // could also set export/import settings
736 * Returns the preferred feature colour configuration if there is one, else
741 public FeatureSettingsModelI getFeatureColourScheme()
746 public DataSourceType getDataSourceType()
748 return dataSourceType;
752 * Returns a buffered reader for the input object. Returns null, or throws
753 * IOException, on failure.
756 * a File, or a String which is a name of a file
759 * @throws IOException
761 public BufferedReader getBufferedReader(Object file,
762 DataSourceType sourceType) throws IOException
764 BufferedReader in = null;
770 if (file instanceof String)
772 return new BufferedReader(new FileReader((String) file));
774 bytes = Platform.getFileBytes((File) file);
777 return new BufferedReader(
778 new InputStreamReader(new ByteArrayInputStream(bytes)));
780 return new BufferedReader(new FileReader((File) file));
782 URL url = new URL(file.toString());
783 in = new BufferedReader(new InputStreamReader(url.openStream()));
785 case RELATIVE_URL: // JalviewJS only
786 bytes = Platform.getFileAsBytes(file.toString());
789 in = new BufferedReader(
790 new InputStreamReader(new ByteArrayInputStream(bytes)));
794 in = new BufferedReader(new StringReader(file.toString()));
797 InputStream is = getClass().getResourceAsStream("/" + file);
800 in = new BufferedReader(new InputStreamReader(is));