Example #1
1
 /**
  * Receives a char array and stores it into the PipedReader. This called by PipedWriter.write()
  * when writes occur.
  *
  * <p>If the buffer is full and the thread sending #receive is interrupted, the
  * InterruptedIOException will be thrown.
  *
  * @param chars the char array to store into the pipe.
  * @param offset offset to start reading from
  * @param count total characters to read
  * @throws IOException If the stream is already closed or another IOException occurs.
  */
 void receive(char[] chars, int offset, int count) throws IOException {
   synchronized (lock) {
     if (data == null) {
       throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
     }
     if (lastReader != null && !lastReader.isAlive()) {
       throw new IOException(Messages.getString("luni.CE")); // $NON-NLS-1$
     }
     /**
      * Set the last thread to be writing on this PipedWriter. If lastWriter dies while someone is
      * waiting to read an IOException of "Pipe broken" will be thrown in read()
      */
     lastWriter = Thread.currentThread();
     while (count > 0) {
       try {
         while (data != null && out == in) {
           lock.notifyAll();
           wait(1000);
           if (lastReader != null && !lastReader.isAlive()) {
             throw new IOException(Messages.getString("luni.CE")); // $NON-NLS-1$
           }
         }
       } catch (InterruptedException e) {
         throw new InterruptedIOException();
       }
       if (data == null) {
         break;
       }
       if (in == -1) {
         in = 0;
       }
       if (in >= out) {
         int length = data.length - in;
         if (count < length) {
           length = count;
         }
         System.arraycopy(chars, offset, data, in, length);
         offset += length;
         count -= length;
         in += length;
         if (in == data.length) {
           in = 0;
         }
       }
       if (count > 0 && in != out) {
         int length = out - in;
         if (count < length) {
           length = count;
         }
         System.arraycopy(chars, offset, data, in, length);
         offset += length;
         count -= length;
         in += length;
       }
     }
     if (count == 0) {
       return;
     }
   }
 }
  private void init(final String path, String pathActions) {
    if (pathActions == null || pathActions.equals("")) { // $NON-NLS-1$
      throw new IllegalArgumentException(Messages.getString("luni.B7")); // $NON-NLS-1$
    }
    this.actions = toCanonicalActionString(pathActions);

    if (path == null) {
      throw new NullPointerException(Messages.getString("luni.B8")); // $NON-NLS-1$
    }
    if (path.equals("<<ALL FILES>>")) { // $NON-NLS-1$
      includeAll = true;
    } else {
      canonPath =
          AccessController.doPrivileged(
              new PrivilegedAction<String>() {
                public String run() {
                  try {
                    return new File(path).getCanonicalPath();
                  } catch (IOException e) {
                    return path;
                  }
                }
              });
      if (path.equals("*") || path.endsWith(File.separator + "*")) { // $NON-NLS-1$ //$NON-NLS-2$
        allDir = true;
      }
      if (path.equals("-") || path.endsWith(File.separator + "-")) { // $NON-NLS-1$ //$NON-NLS-2$
        allSubdir = true;
      }
    }
  }
Example #3
0
 /**
  * Establishes the connection to the PipedWriter.
  *
  * @throws IOException If this Reader is already connected.
  */
 void establishConnection() throws IOException {
   synchronized (lock) {
     if (data == null) {
       throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
     }
     if (isConnected) {
       throw new IOException(Messages.getString("luni.D0")); // $NON-NLS-1$
     }
     isConnected = true;
   }
 }
 /** Answer an error string corresponding to the given error value. */
 public String getErrorString(int error) {
   switch (error) {
     case RETURN_FAILURE:
       return Messages.getString("luni.14"); // $NON-NLS-1$
     case RETURN_CANNOT_CONNECT_TO_IDENTD:
       return Messages.getString("luni.15"); // $NON-NLS-1$
     case RETURN_DIFFERENT_USER_IDS:
       return Messages.getString("luni.16"); // $NON-NLS-1$
     default:
       return Messages.getString("luni.17"); // $NON-NLS-1$
   }
 }
Example #5
0
 /**
  * Indicates whether this reader is ready to be read without blocking. Returns {@code true} if
  * this reader will not block when {@code read} is called, {@code false} if unknown or blocking
  * will occur. This implementation returns {@code true} if the internal buffer contains characters
  * that can be read.
  *
  * @return always {@code false}.
  * @throws IOException if this reader is closed or not connected, or if some other I/O error
  *     occurs.
  * @see #read()
  * @see #read(char[], int, int)
  */
 @Override
 public boolean ready() throws IOException {
   synchronized (lock) {
     if (!isConnected) {
       throw new IOException(Messages.getString("luni.D1")); // $NON-NLS-1$
     }
     if (data == null) {
       throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
     }
     return in != -1;
   }
 }
Example #6
0
  /**
   * Gets the specific network interface according to the given address.
   *
   * @param address the address to identify the searched network interface.
   * @return the network interface with the specified address if one exists or {@code null}
   *     otherwise.
   * @throws SocketException if an error occurs while getting the network interface information.
   * @throws NullPointerException if the given interface address is invalid.
   */
  public static NetworkInterface getByInetAddress(InetAddress address) throws SocketException {

    if (address == null) {
      throw new NullPointerException(Messages.getString("luni.68")); // $NON-NLS-1$
    }

    /*
     * get the list of interfaces, and then loop through the list. For each
     * interface loop through the associated set of internet addresses and
     * see if one matches. If so return that network interface
     */
    Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
    if (interfaces != null) {
      while (interfaces.hasMoreElements()) {
        NetworkInterface netif = interfaces.nextElement();
        /*
         * to be compatible use the raw addresses without any security
         * filtering
         */
        // Enumeration netifAddresses = netif.getInetAddresses();
        if ((netif.addresses != null) && (netif.addresses.length != 0)) {
          Enumeration<InetAddress> netifAddresses =
              (new Vector<InetAddress>(Arrays.asList(netif.addresses))).elements();
          if (netifAddresses != null) {
            while (netifAddresses.hasMoreElements()) {
              if (address.equals(netifAddresses.nextElement())) {
                return netif;
              }
            }
          }
        }
      }
    }
    return null;
  }
Example #7
0
 private static ResourceBundle getBundleImpl(String bundleName, Locale locale, ClassLoader loader)
     throws MissingResourceException {
   if (bundleName != null) {
     ResourceBundle bundle;
     if (!locale.equals(Locale.getDefault())) {
       String localeName = locale.toString();
       if (localeName.length() > 0) {
         localeName = UNDER_SCORE + localeName;
       }
       if ((bundle = handleGetBundle(bundleName, localeName, false, loader)) != null) {
         return bundle;
       }
     }
     String localeName = Locale.getDefault().toString();
     if (localeName.length() > 0) {
       localeName = UNDER_SCORE + localeName;
     }
     if ((bundle = handleGetBundle(bundleName, localeName, true, loader)) != null) {
       return bundle;
     }
     throw new MissingResourceException(
         Messages.getString("luni.3A", bundleName, locale),
         bundleName + '_' + locale, // $NON-NLS-1$
         EMPTY_STRING);
   }
   throw new NullPointerException();
 }
Example #8
0
 void flush() throws IOException {
   synchronized (lock) {
     if (isClosed) {
       throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
     }
     lock.notifyAll();
   }
 }
Example #9
0
 /**
  * Receives a char and stores it into the PipedReader. This called by PipedWriter.write() when
  * writes occur.
  *
  * <p>If the buffer is full and the thread sending #receive is interrupted, the
  * InterruptedIOException will be thrown.
  *
  * @param oneChar the char to store into the pipe.
  * @throws IOException If the stream is already closed or another IOException occurs.
  */
 void receive(char oneChar) throws IOException {
   synchronized (lock) {
     if (data == null) {
       throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
     }
     if (lastReader != null && !lastReader.isAlive()) {
       throw new IOException(Messages.getString("luni.CE")); // $NON-NLS-1$
     }
     /*
      * Set the last thread to be writing on this PipedWriter. If
      * lastWriter dies while someone is waiting to read an IOException
      * of "Pipe broken" will be thrown in read()
      */
     lastWriter = Thread.currentThread();
     try {
       while (data != null && out == in) {
         lock.notifyAll();
         wait(1000);
         if (lastReader != null && !lastReader.isAlive()) {
           throw new IOException(Messages.getString("luni.CE")); // $NON-NLS-1$
         }
       }
     } catch (InterruptedException e) {
       throw new InterruptedIOException();
     }
     if (data != null) {
       if (in == -1) {
         in = 0;
       }
       data[in++] = oneChar;
       if (in == data.length) {
         in = 0;
       }
       return;
     }
   }
 }
Example #10
0
 /**
  * Returns the named resource from this {@code ResourceBundle}. If the resource cannot be found in
  * this bundle, it falls back to the parent bundle (if it's not null) by calling the {@link
  * #handleGetObject} method. If the resource still can't be found it throws a {@code
  * MissingResourceException}.
  *
  * @param key the name of the resource.
  * @return the resource object.
  * @throws MissingResourceException if the resource is not found.
  */
 public final Object getObject(String key) {
   ResourceBundle last, theParent = this;
   do {
     Object result = theParent.handleGetObject(key);
     if (result != null) {
       return result;
     }
     last = theParent;
     theParent = theParent.parent;
   } while (theParent != null);
   throw new MissingResourceException(
       Messages.getString("luni.3A", last.getClass().getName(), key),
       last.getClass().getName(),
       key); //$NON-NLS-1$
 }
Example #11
0
  /**
   * Gets the specific network interface according to a given name.
   *
   * @param interfaceName the name to identify the searched network interface.
   * @return the network interface with the specified name if one exists or {@code null} otherwise.
   * @throws SocketException if an error occurs while getting the network interface information.
   * @throws NullPointerException if the given interface's name is {@code null}.
   */
  public static NetworkInterface getByName(String interfaceName) throws SocketException {

    if (interfaceName == null) {
      throw new NullPointerException(Messages.getString("luni.6D")); // $NON-NLS-1$
    }

    /*
     * get the list of interfaces, and then loop through the list to look
     * for one with a matching name
     */
    Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
    if (interfaces != null) {
      while (interfaces.hasMoreElements()) {
        NetworkInterface netif = interfaces.nextElement();
        if (netif.getName().equals(interfaceName)) {
          return netif;
        }
      }
    }
    return null;
  }
 /**
  * Returns the numerical representation of the argument.
  *
  * @param actionNames the action names
  * @return the action mask
  */
 private int getMask(String actionNames) {
   int actionInt = 0, head = 0, tail = 0;
   do {
     tail = actionNames.indexOf(",", head); // $NON-NLS-1$
     String action =
         tail > 0 ? actionNames.substring(head, tail).trim() : actionNames.substring(head).trim();
     if (action.equals("read")) { // $NON-NLS-1$
       actionInt |= 8;
     } else if (action.equals("write")) { // $NON-NLS-1$
       actionInt |= 4;
     } else if (action.equals("execute")) { // $NON-NLS-1$
       actionInt |= 2;
     } else if (action.equals("delete")) { // $NON-NLS-1$
       actionInt |= 1;
     } else {
       throw new IllegalArgumentException(Messages.getString("luni.B9", action)); // $NON-NLS-1$
     }
     head = tail + 1;
   } while (tail > 0);
   return actionInt;
 }
Example #13
0
 /**
  * Finds the named resource bundle for the specified {@code Locale} and {@code ClassLoader}.
  *
  * <p>The passed base name and {@code Locale} are used to create resource bundle names. The first
  * name is created by concatenating the base name with the result of {@link Locale#toString()}.
  * From this name all parent bundle names are derived. Then the same thing is done for the default
  * {@code Locale}. This results in a list of possible bundle names.
  *
  * <p><strong>Example</strong> For the basename "BaseName", the {@code Locale} of the German part
  * of Switzerland (de_CH) and the default {@code Locale} en_US the list would look something like
  * this:
  *
  * <ol>
  *   <li>BaseName_de_CH
  *   <li>BaseName_de
  *   <li>Basename_en_US
  *   <li>Basename_en
  *   <li>BaseName
  * </ol>
  *
  * This list also shows the order in which the bundles will be searched for a requested resource
  * in the German part of Switzerland (de_CH).
  *
  * <p>As a first step, this method tries to instantiate a {@code ResourceBundle} with the names
  * provided. If such a class can be instantiated and initialized, it is returned and all the
  * parent bundles are instantiated too. If no such class can be found this method tries to load a
  * {@code .properties} file with the names by replacing dots in the base name with a slash and by
  * appending "{@code .properties}" at the end of the string. If such a resource can be found by
  * calling {@link ClassLoader#getResource(String)} it is used to initialize a {@link
  * PropertyResourceBundle}. If this succeeds, it will also load the parents of this {@code
  * ResourceBundle}.
  *
  * <p>For compatibility with older code, the bundle name isn't required to be a fully qualified
  * class name. It's also possible to directly pass the path to a properties file (without a file
  * extension).
  *
  * @param bundleName the name of the {@code ResourceBundle}.
  * @param locale the {@code Locale}.
  * @param loader the {@code ClassLoader} to use.
  * @return the requested {@code ResourceBundle}.
  * @throws MissingResourceException if the {@code ResourceBundle} cannot be found.
  */
 public static ResourceBundle getBundle(String bundleName, Locale locale, ClassLoader loader)
     throws MissingResourceException {
   if (loader == null) {
     throw new NullPointerException();
   }
   if (bundleName != null) {
     ResourceBundle bundle;
     if (!locale.equals(Locale.getDefault())) {
       if ((bundle = handleGetBundle(bundleName, UNDER_SCORE + locale, false, loader)) != null) {
         return bundle;
       }
     }
     if ((bundle = handleGetBundle(bundleName, UNDER_SCORE + Locale.getDefault(), true, loader))
         != null) {
       return bundle;
     }
     throw new MissingResourceException(
         Messages.getString("luni.3A", bundleName, locale),
         bundleName + '_' + locale, // $NON-NLS-1$
         EMPTY_STRING);
   }
   throw new NullPointerException();
 }
Example #14
0
  /**
   * Reads at most {@code count} characters from this reader and stores them in the character array
   * {@code buffer} starting at {@code offset}. If there is no data in the pipe, this method blocks
   * until at least one byte has been read, the end of the reader is detected or an exception is
   * thrown.
   *
   * <p>Separate threads should be used to read from a {@code PipedReader} and to write to the
   * connected {@link PipedWriter}. If the same thread is used, a deadlock may occur.
   *
   * @param buffer the character array in which to store the characters read.
   * @param offset the initial position in {@code bytes} to store the characters read from this
   *     reader.
   * @param count the maximum number of characters to store in {@code buffer}.
   * @return the number of characters read or -1 if the end of the reader has been reached.
   * @throws IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code
   *     offset + count} is greater than the size of {@code buffer}.
   * @throws InterruptedIOException if the thread reading from this reader is interrupted.
   * @throws IOException if this reader is closed or not connected to a writer, or if the thread
   *     writing to the connected writer is no longer alive.
   */
  @Override
  public int read(char[] buffer, int offset, int count) throws IOException {
    synchronized (lock) {
      if (!isConnected) {
        throw new IOException(Messages.getString("luni.D1")); // $NON-NLS-1$
      }
      if (data == null) {
        throw new IOException(Messages.getString("luni.CF")); // $NON-NLS-1$
      }
      // avoid int overflow
      if (offset < 0 || count > buffer.length - offset || count < 0) {
        throw new IndexOutOfBoundsException();
      }
      if (count == 0) {
        return 0;
      }
      /**
       * Set the last thread to be reading on this PipedReader. If lastReader dies while someone is
       * waiting to write an IOException of "Pipe broken" will be thrown in receive()
       */
      lastReader = Thread.currentThread();
      try {
        boolean first = true;
        while (in == -1) {
          // Are we at end of stream?
          if (isClosed) {
            return -1;
          }
          if (!first && lastWriter != null && !lastWriter.isAlive()) {
            throw new IOException(Messages.getString("luni.CE")); // $NON-NLS-1$
          }
          first = false;
          // Notify callers of receive()
          lock.notifyAll();
          lock.wait(1000);
        }
      } catch (InterruptedException e) {
        throw new InterruptedIOException();
      }

      int copyLength = 0;
      /* Copy chars from out to end of buffer first */
      if (out >= in) {
        copyLength = count > data.length - out ? data.length - out : count;
        System.arraycopy(data, out, buffer, offset, copyLength);
        out += copyLength;
        if (out == data.length) {
          out = 0;
        }
        if (out == in) {
          // empty buffer
          in = -1;
          out = 0;
        }
      }

      /*
       * Did the read fully succeed in the previous copy or is the buffer
       * empty?
       */
      if (copyLength == count || in == -1) {
        return copyLength;
      }

      int charsCopied = copyLength;
      /* Copy bytes from 0 to the number of available bytes */
      copyLength = in - out > count - copyLength ? count - copyLength : in - out;
      System.arraycopy(data, out, buffer, offset + charsCopied, copyLength);
      out += copyLength;
      if (out == in) {
        // empty buffer
        in = -1;
        out = 0;
      }
      return charsCopied + copyLength;
    }
  }