|
| 1 | +/** |
| 2 | + * Copyright 2017 EMC Corporation. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package com.emc.ecs.nfsclient.nfs.io; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +import com.emc.ecs.nfsclient.nfs.NfsReadResponse; |
| 20 | +import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3; |
| 21 | +import com.emc.ecs.nfsclient.rpc.CredentialUnix; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author seibed |
| 25 | + * |
| 26 | + */ |
| 27 | +public class FileReadingTest { |
| 28 | + |
| 29 | + public static void main(String[] args) { |
| 30 | + String export = args[0]; |
| 31 | + String fileName = args[1]; |
| 32 | + boolean useStream = Boolean.valueOf(args[2]); |
| 33 | + try { |
| 34 | + if (useStream) { |
| 35 | + testStreamReading(export, fileName); |
| 36 | + } else { |
| 37 | + testReading(export, fileName); |
| 38 | + } |
| 39 | + } catch (Exception e) { |
| 40 | + System.out.println(e.getMessage()); |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public static void testStreamReading(String export, String fileName) |
| 46 | + throws Exception { |
| 47 | + Nfs3 nfs3 = new Nfs3(export, new CredentialUnix(0, 0, null), 3); |
| 48 | + NfsFileInputStream inputStream = null; |
| 49 | + try { |
| 50 | + NfsFile nfsFile = new Nfs3File(nfs3, fileName); |
| 51 | + int fileLength = (int) nfsFile.length(); |
| 52 | + inputStream = new NfsFileInputStream(nfsFile); |
| 53 | + byte[] buffer = new byte[fileLength]; |
| 54 | + System.out.println("Testing stream reading"); |
| 55 | + int bytesRead = inputStream.read(buffer); |
| 56 | + if (NfsFileInputStream.EOF != inputStream.read()) { |
| 57 | + throw new Exception("Reading error - should have been at the end of the file"); |
| 58 | + } |
| 59 | + if (fileLength != bytesRead) { |
| 60 | + throw new Exception("Reading error - read " + bytesRead |
| 61 | + + " bytes, should have been " + fileLength); |
| 62 | + } |
| 63 | + System.out.println("Success!"); |
| 64 | + } catch (Exception e) { |
| 65 | + System.out.println(e.getMessage()); |
| 66 | + } finally { |
| 67 | + if (inputStream != null) { |
| 68 | + try { |
| 69 | + inputStream.close(); |
| 70 | + } catch (IOException e) { |
| 71 | + // e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void testReading(String export, String fileName) |
| 78 | + throws Exception { |
| 79 | + Nfs3 nfs3 = new Nfs3(export, new CredentialUnix(0, 0, null), 3); |
| 80 | + try { |
| 81 | + NfsFile nfsFile = new Nfs3File(nfs3, fileName); |
| 82 | + int fileLength = (int) nfsFile.length(); |
| 83 | + byte[] buffer = new byte[fileLength]; |
| 84 | + System.out.println("Testing with plain read"); |
| 85 | + NfsReadResponse response = nfsFile.read(0, buffer.length, buffer, 0); |
| 86 | + if (!response.isEof()) { |
| 87 | + throw new Exception("Reading error - should have been at the end of the file"); |
| 88 | + } |
| 89 | + if (fileLength != response.getBytesRead()) { |
| 90 | + throw new Exception("Reading error - read " + response.getBytesRead() |
| 91 | + + " bytes, should have been " + fileLength); |
| 92 | + } |
| 93 | + System.out.println("Success!"); |
| 94 | + } catch (Exception e) { |
| 95 | + System.out.println(e.getMessage()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments