Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / cli / Console.java
blob31fe3126dda0bfc465cd7bf7b598434b1eef014a
1 package ch.cyberduck.cli;
3 /*
4 * Copyright (c) 2002-2014 David Kocher. All rights reserved.
5 * http://cyberduck.io/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch.cyberduck.core.Factory;
22 import ch.cyberduck.core.exception.ConnectionCanceledException;
23 import ch.cyberduck.core.exception.LoginCanceledException;
25 import org.apache.commons.lang3.StringUtils;
26 import org.fusesource.jansi.AnsiConsole;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.PrintStream;
32 import java.io.PrintWriter;
33 import java.util.Arrays;
34 import java.util.concurrent.Semaphore;
36 /**
37 * @version $Id$
39 public class Console {
41 private final java.io.Console console
42 = System.console();
44 private final PrintStream out
45 = AnsiConsole.out();
47 private static final Semaphore lock
48 = new Semaphore(1);
50 public String readLine(String format, Object... args) throws ConnectionCanceledException {
51 if(console != null) {
52 return this.wrap(console.readLine(format, args));
54 this.printf(format, args);
55 final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
56 try {
57 return this.wrap(reader.readLine());
59 catch(IOException e) {
60 throw new ConnectionCanceledException(e);
64 public char[] readPassword(String format, Object... args) throws ConnectionCanceledException {
65 if(console != null) {
66 return this.wrap(console.readPassword(format, args));
68 final String line = this.readLine(format, args);
69 if(StringUtils.isBlank(line)) {
70 throw new LoginCanceledException();
72 return line.toCharArray();
75 public void printf(final String format, Object... args) {
76 if(StringUtils.isEmpty(format)) {
77 return;
79 try {
80 lock.acquire();
81 if(console != null) {
82 switch(Factory.Platform.getDefault()) {
83 case windows:
84 break;
85 default:
86 final PrintWriter writer = console.writer();
87 if(Arrays.asList(args).isEmpty()) {
88 writer.print(format);
90 else {
91 writer.printf(format, args);
93 writer.flush();
94 return;
97 if(Arrays.asList(args).isEmpty()) {
98 out.printf(format);
100 else {
101 out.printf(format, args);
103 out.flush();
105 catch(InterruptedException e) {
108 finally {
109 lock.release();
113 private String wrap(final String input) throws ConnectionCanceledException {
114 if(null == input) {
115 throw new ConnectionCanceledException();
117 return input;
120 private char[] wrap(final char[] input) throws ConnectionCanceledException {
121 if(null == input) {
122 throw new ConnectionCanceledException();
124 return input;