// Path: src/main/java/org/gm4java/engine/GMConnection.java
// public interface GMConnection extends GMExecutor {
//     /**
//      * Close {@linkplain GMConnection} and destroy the underlying GraphicsMagick process.
//      * 
//      * @throws GMServiceException
//      *             when there is error communicating with the underlying GraphicsMagick process
//      */
//     void close() throws GMServiceException;
// }
// 
// Path: src/main/java/org/gm4java/engine/GMException.java
// public class GMException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Construct a new instance of exception.
//      * 
//      * @param message
//      *            the detailed message returned by GraphicsMagick
//      */
//     public GMException(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: src/main/java/org/gm4java/engine/GMServiceException.java
// public class GMServiceException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Constructs a new exception with given message.
//      * 
//      * @param message
//      *            the detailed message of the exception.
//      */
//     public GMServiceException(String message) {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new exception with given message and cause.
//      * 
//      * @param message
//      *            the detailed message of the exception.
//      * @param cause
//      *            the cause of the error.
//      */
//     public GMServiceException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


import org.gm4java.engine.GMConnection;
import org.gm4java.engine.GMException;
import org.gm4java.engine.GMServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/*
 * Copyright (c) 2011 Original Authors
 *
 * Licensed 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.gm4java.engine.support;




/**
 * 
 * A implementation of {@link GMConnection} that serves as base of other implementation, and also used by
 * {@link SimpleGMService}.
 * 
 * @author Kenneth Xu
 * 
 */
class BasicGMConnection implements GMConnection {
    private static final List<String> empty = Arrays.asList(new String[0]);
    private static final int NORMAL_BUFFER_SIZE = 4096;
    private static final String EOL = System.getProperty("line.separator");
    private static final int EOL_SIZE = EOL.length();
    private ReaderWriterProcess process;
    private final StringBuffer sb = new StringBuffer();

    public BasicGMConnection(@Nonnull ReaderWriterProcess process) throws GMServiceException {
        if (process == null) throw new NullPointerException("process");
        this.process = process;
    }

    @Override
    public final String execute(@Nonnull String command, @CheckForNull String... arguments) throws IOException,
            GMException, GMServiceException {
        if (command == null) throw new NullPointerException("Argument 'command' must not be null");
        return execute(command, arguments == null || arguments.length == 0 ? empty : Arrays.asList(arguments));
    }

    @Override
    public final String execute(@Nonnull List<String> command) throws IOException, GMException, GMServiceException {
// Path: src/main/java/org/gm4java/engine/GMConnection.java
// public interface GMConnection extends GMExecutor {
//     /**
//      * Close {@linkplain GMConnection} and destroy the underlying GraphicsMagick process.
//      * 
//      * @throws GMServiceException
//      *             when there is error communicating with the underlying GraphicsMagick process
//      */
//     void close() throws GMServiceException;
// }
// 
// Path: src/main/java/org/gm4java/engine/GMException.java
// public class GMException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Construct a new instance of exception.
//      * 
//      * @param message
//      *            the detailed message returned by GraphicsMagick
//      */
//     public GMException(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: src/main/java/org/gm4java/engine/GMServiceException.java
// public class GMServiceException extends Exception {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Constructs a new exception with given message.
//      * 
//      * @param message
//      *            the detailed message of the exception.
//      */
//     public GMServiceException(String message) {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new exception with given message and cause.
//      * 
//      * @param message
//      *            the detailed message of the exception.
//      * @param cause
//      *            the cause of the error.
//      */
//     public GMServiceException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


// Path: src/main/java/org/gm4java/engine/support/BasicGMConnection.java
import org.gm4java.engine.GMConnection;
import org.gm4java.engine.GMException;
import org.gm4java.engine.GMServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/*
 * Copyright (c) 2011 Original Authors
 *
 * Licensed 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.gm4java.engine.support;




/**
 * 
 * A implementation of {@link GMConnection} that serves as base of other implementation, and also used by
 * {@link SimpleGMService}.
 * 
 * @author Kenneth Xu
 * 
 */
class BasicGMConnection implements GMConnection {
    private static final List<String> empty = Arrays.asList(new String[0]);
    private static final int NORMAL_BUFFER_SIZE = 4096;
    private static final String EOL = System.getProperty("line.separator");
    private static final int EOL_SIZE = EOL.length();
    private ReaderWriterProcess process;
    private final StringBuffer sb = new StringBuffer();

    public BasicGMConnection(@Nonnull ReaderWriterProcess process) throws GMServiceException {
        if (process == null) throw new NullPointerException("process");
        this.process = process;
    }

    @Override
    public final String execute(@Nonnull String command, @CheckForNull String... arguments) throws IOException,
            GMException, GMServiceException {
        if (command == null) throw new NullPointerException("Argument 'command' must not be null");
        return execute(command, arguments == null || arguments.length == 0 ? empty : Arrays.asList(arguments));
    }

    @Override
    public final String execute(@Nonnull List<String> command) throws IOException, GMException, GMServiceException {
        if (command == null) throw new NullPointerException("Argument 'command' must not be null");