+// Helper classes and methods implementation
+
+#ifdef MINGW_LIBGCJ_UNICODE
+
+// We're using the OS W (UNICODE) API, which means that we're speaking
+// the same language....
+jstring
+_Jv_Win32NewString (LPCTSTR pcsz)
+{
+ return JvNewString ((jchar*) pcsz, _tcslen (pcsz));
+}
+
+#else
+
+// We're using the OS A functions, which means we need to translate between
+// UNICODE and the native character set.
+
+// First, let's set up some helper translation functions....
+
+// Converts the native string to any specified jstring, returning the
+// length of the jstring. If the specified jstring is null, we simply
+// compute and return the length.
+static int nativeToUnicode(LPCSTR pcsz, jstring jstr = 0)
+{
+ jchar* buf = 0;
+ int len = 0;
+ if (jstr)
+ {
+ len = jstr->length();
+ buf = JvGetStringChars(jstr);
+ }
+ return ::MultiByteToWideChar(GetACP(), 0, pcsz,
+ strlen(pcsz), (LPWSTR) buf, len);
+}
+
+// Does the inverse of nativeToUnicode, with the same calling semantics.
+static int unicodeToNative(jstring jstr, LPSTR buf, int buflen)
+{
+ return ::WideCharToMultiByte(GetACP(), 0, (LPWSTR) JvGetStringChars(jstr),
+ jstr->length(), buf, buflen, NULL, NULL);
+}
+
+// Convenience function when the caller only wants to compute the length
+// of the native string.
+static int unicodeToNative(jstring jstr)
+{
+ return unicodeToNative(jstr, 0, 0);
+}
+
+jstring
+_Jv_Win32NewString (LPCTSTR pcsz)
+{
+ // Compute the length, allocate the jstring, then perform the conversion.
+ int len = nativeToUnicode(pcsz);
+ jstring jstr = JvAllocString(len);
+ nativeToUnicode(pcsz, jstr);
+ return jstr;
+}
+
+#endif // MINGW_LIBGCJ_UNICODE
+
+// class _Jv_Win32TempString
+_Jv_Win32TempString::_Jv_Win32TempString(jstring jstr):
+ buf_(0)
+{
+ if (jstr == 0)
+ return;
+
+ // We need space for the string length plus a null terminator.
+ // Determine whether to use our stack-allocated buffer or a heap-
+ // allocated one.
+#ifdef MINGW_LIBGCJ_UNICODE
+ // A UNICODE character is a UNICODE character is a UNICODE character....
+ int len = jstr->length();
+#else
+ // Compute the length of the native character string.
+ int len = unicodeToNative(jstr);
+#endif // MINGW_LIBGCJ_UNICODE
+
+ int bytesNeeded = (len + 1) * sizeof(TCHAR);
+ if (bytesNeeded <= (int) sizeof(stackbuf_))
+ buf_ = stackbuf_;
+ else
+ buf_ = (LPTSTR) _Jv_Malloc(bytesNeeded);
+
+#ifdef MINGW_LIBGCJ_UNICODE
+ // Copy the UNICODE characters to our buffer.
+ _tcsncpy(buf_, (LPCTSTR) JvGetStringChars (jstr), len);
+#else
+ // Convert the UNICODE string to a native one.
+ unicodeToNative(jstr, buf_, len);
+#endif // MINGW_LIBGCJ_UNICODE
+
+ buf_[len] = 0;
+}
+
+_Jv_Win32TempString::~_Jv_Win32TempString()
+{
+ if (buf_ && buf_ != stackbuf_)
+ _Jv_Free (buf_);
+}
+
+// class WSAEventWrapper
+WSAEventWrapper::WSAEventWrapper ():
+ m_hEvent(0),
+ m_fd(0),
+ m_dwSelFlags(0)
+{}
+
+WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags):
+ m_hEvent(0),
+ m_fd(0),
+ m_dwSelFlags(0)
+{
+ init(fd, dwSelFlags);
+}
+
+void WSAEventWrapper::init(int fd, DWORD dwSelFlags)
+{
+ m_fd = fd;
+ m_dwSelFlags = dwSelFlags;
+ m_hEvent = WSACreateEvent ();
+ if (dwSelFlags)
+ WSAEventSelect(fd, m_hEvent, dwSelFlags);
+}
+
+WSAEventWrapper::~WSAEventWrapper ()
+{
+ if (m_dwSelFlags)
+ {
+ WSAEventSelect(m_fd, m_hEvent, 0);
+ if (m_dwSelFlags & (FD_ACCEPT | FD_CONNECT))
+ {
+ // Set the socket back to non-blocking mode.
+ // Ignore any error since we're in a destructor.
+ unsigned long lSockOpt = 0L;
+ // blocking mode
+ ::ioctlsocket (m_fd, FIONBIO, &lSockOpt);
+ }
+ }
+ WSACloseEvent (m_hEvent);
+}
+
+// Error string text.
+jstring
+_Jv_WinStrError (LPCTSTR lpszPrologue, int nErrorCode)
+{
+ LPTSTR lpMsgBuf = 0;
+
+ DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS;
+
+ FormatMessage (dwFlags,
+ NULL,
+ (DWORD) nErrorCode,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR) &lpMsgBuf,
+ 0,
+ NULL);
+
+ jstring ret;
+ if (lpszPrologue)
+ {
+ LPTSTR lpszTemp =
+ (LPTSTR) _Jv_Malloc ((_tcslen (lpszPrologue) +
+ _tcslen (lpMsgBuf) + 3) * sizeof(TCHAR) );
+ _tcscpy (lpszTemp, lpszPrologue);
+ _tcscat (lpszTemp, _T(": "));
+ _tcscat (lpszTemp, lpMsgBuf);
+ ret = _Jv_Win32NewString (lpszTemp);
+ _Jv_Free (lpszTemp);
+ }
+ else
+ {
+ ret = _Jv_Win32NewString (lpMsgBuf);
+ }
+
+ LocalFree(lpMsgBuf);
+ return ret;
+}
+
+jstring
+_Jv_WinStrError (int nErrorCode)
+{
+ return _Jv_WinStrError (0, nErrorCode);
+}
+
+void _Jv_ThrowIOException (DWORD dwErrorCode)
+{
+ throw new java::io::IOException (_Jv_WinStrError (dwErrorCode));
+}
+
+void _Jv_ThrowIOException()
+{
+ DWORD dwErrorCode = WSAGetLastError ();
+ _Jv_ThrowIOException (dwErrorCode);
+}
+
+void _Jv_ThrowSocketException (DWORD dwErrorCode)
+{
+ throw new java::net::SocketException (_Jv_WinStrError (dwErrorCode));
+}
+
+void _Jv_ThrowSocketException()
+{
+ DWORD dwErrorCode = WSAGetLastError ();
+ _Jv_ThrowSocketException (dwErrorCode);
+}
+