|
15 | 15 | ******************************************************************************/
|
16 | 16 | package org.eclipse.core.filesystem;
|
17 | 17 |
|
| 18 | +import java.io.IOException; |
18 | 19 | import java.io.InputStream;
|
19 | 20 | import java.io.OutputStream;
|
20 | 21 | import java.net.URI;
|
21 | 22 | import org.eclipse.core.filesystem.provider.FileStore;
|
22 | 23 | import org.eclipse.core.internal.filesystem.FileStoreUtil;
|
23 |
| -import org.eclipse.core.runtime.*; |
| 24 | +import org.eclipse.core.runtime.CoreException; |
| 25 | +import org.eclipse.core.runtime.IAdaptable; |
| 26 | +import org.eclipse.core.runtime.IPath; |
| 27 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 28 | +import org.eclipse.core.runtime.Status; |
24 | 29 |
|
25 | 30 | /**
|
26 | 31 | * A file store is responsible for storage and retrieval of a single file in some file system.
|
@@ -412,6 +417,29 @@ public interface IFileStore extends IAdaptable {
|
412 | 417 | */
|
413 | 418 | public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException;
|
414 | 419 |
|
| 420 | + /** |
| 421 | + * Returns the bytes contents of this file. This method is not intended for reading in |
| 422 | + * large files that do not fit in a byte array. Preferable over calling |
| 423 | + * {@code openInputStream(options, monitor).readAllBytes()} |
| 424 | + * @param options bit-wise or of option flag constants (currently only {@link EFS#NONE} |
| 425 | + * is applicable). |
| 426 | + * @param monitor a progress monitor, or <code>null</code> if progress |
| 427 | + * reporting and cancellation are not desired |
| 428 | + * @return contents of this file as byte array. |
| 429 | + * @throws CoreException if this method fails. |
| 430 | + * @see #openInputStream(int, IProgressMonitor) |
| 431 | + * @since 1.11 |
| 432 | + */ |
| 433 | + public default byte[] readAllBytes(int options, IProgressMonitor monitor) throws CoreException { |
| 434 | + // Default implementation is meant to be overridden for local files |
| 435 | + // as Files.readAllBytes is ~ 1.5 times faster |
| 436 | + try (InputStream stream = openInputStream(options, monitor)) { |
| 437 | + return stream.readAllBytes(); |
| 438 | + } catch (IOException e) { |
| 439 | + throw new CoreException(Status.error("Error reading " + getName(), e)); //$NON-NLS-1$ |
| 440 | + } |
| 441 | + } |
| 442 | + |
415 | 443 | /**
|
416 | 444 | * Returns an open output stream on the contents of this file. The number of
|
417 | 445 | * concurrently open streams depends on implementation and can be limited.
|
@@ -449,6 +477,31 @@ public interface IFileStore extends IAdaptable {
|
449 | 477 | */
|
450 | 478 | public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException;
|
451 | 479 |
|
| 480 | + /** |
| 481 | + * Writes the contents to this file. |
| 482 | + * <p> |
| 483 | + * The {@link EFS#APPEND} update flag controls where |
| 484 | + * output is written to the file. If this flag is specified, content written |
| 485 | + * to the stream will be appended to the end of the file. If this flag is |
| 486 | + * not specified, the contents of the existing file, if any, is truncated to zero |
| 487 | + * and the new output will be written from the start of the file. |
| 488 | + * </p> |
| 489 | + * |
| 490 | + * @param content contents given as byte array |
| 491 | + * @param options bit-wise or of option flag constants ({@link EFS#APPEND}). |
| 492 | + * @param monitor a progress monitor, or <code>null</code> if progress |
| 493 | + * reporting and cancellation are not desired |
| 494 | + * @see #openOutputStream(int, IProgressMonitor) |
| 495 | + * @since 1.11 |
| 496 | + * **/ |
| 497 | + public default void write(byte[] content, int options, IProgressMonitor monitor) throws CoreException { |
| 498 | + try (OutputStream out = openOutputStream(options, monitor)) { |
| 499 | + out.write(content); |
| 500 | + } catch (IOException e) { |
| 501 | + throw new CoreException(Status.error(e.getMessage(), e)); |
| 502 | + } |
| 503 | + } |
| 504 | + |
452 | 505 | /**
|
453 | 506 | * Writes information about this file to the underlying file system. Only
|
454 | 507 | * certain parts of the file information structure can be written using this
|
|
0 commit comments