Github user mike-jumper commented on a diff in the pull request:
https://github.com/apache/incubator-guacamole-client/pull/183#discussion_r144460945
--- Diff:
guacamole-ext/src/main/java/org/apache/guacamole/properties/PrivateKeyGuacamoleProperty.java
---
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.guacamole.properties;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.lang.IllegalArgumentException;
+import java.security.InvalidKeyException;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+import org.apache.guacamole.GuacamoleServerException;
+import org.apache.guacamole.environment.Environment;
+import org.apache.guacamole.environment.LocalEnvironment;
+
+/**
+ * A GuacamoleProperty whose value is derived from a private key file.
+ */
+public abstract class PrivateKeyGuacamoleProperty implements
GuacamoleProperty<PrivateKey> {
+
+ @Override
+ public PrivateKey parseValue(String value) throws
GuacamoleServerException {
+
+ if (value == null || value.isEmpty())
+ return null;
+
+ try {
+
+ // Open and read the file specified in the configuration.
+ File keyFile = new File(value);
+ InputStream keyInput = new BufferedInputStream(new
FileInputStream(keyFile));
+ int keyLength = (int) keyFile.length();
+ final byte[] keyBytes = new byte[keyLength];
--- End diff --
Looks better, however:
1. The duplicate `read()` calls are somewhat odd.
2. This loop implementation will spin indefinitely. When the buffer is
filled, `keyBytes.length - totalBytesRead` becomes 0, in which case the call to
`read()` will be asked to read 0 bytes and will always return 0, never
signalling EOF.
It might be a lot easier to implement correctly if the byte array is
allocated dynamically. That would also allow for cases where the file size is
not known ahead of time, and remove the need to handle unexpected changes in
file size.
---